Impossible to pass a variable in key return in MongoDB or NoSQL DB?
Here's my code:
dob = 1
email = 1
nationality = 1
work = 1
situation = 1
skype = 1
user_id = ObjectId(self.get_argument("id"))
contact_id = ObjectId(self.get_argument("contactId"))
contact_confidentiality = db.cara.users.find_one({"_id" : contact_id}, {"profil.开发者_如何学编程private.confidential" : 1})
confidentiality = []
for i in contact_confidentiality["profil"]["private"]["confidential"]:
if i == "dob":
dob = 0
elif i == "email":
email = 0
elif i == "nationality":
nationality = 0
elif i == "work":
work = 0
elif i == "situation":
situation = 0
elif i == "skype":
skype = 0
elif i == "facebook":
facebook = 0
contact = db.cara.users.find_one({"_id" : contact_id}, {"profil.private.first_name": 1, "profil.private.last_name": 1, "profil.gender": 1, "profil.dob": dob, "profil.nationality": nationality, "profil.work": work, "profil.private.email_address": email, "profil.private.situation": situation, "profil.private.skype": skype})
MongoDB doesn't want to pass in the last line of my code: dob, nationality, work, email. How can I pass variable?
EDIT:
contact_confidentiality = db.cara.users.find_one({"_id" : contact_id}, {"profil.private.confidential" : 1})
projection = {
'profil.private.first_name' :1 ,
'profil.private.last_name' : 1,
'profil.gender' : 1,
'profil.dob' : 1,
'profil.private.email_address' : 1,
'profil.nationality' : 1,
'profil.work' : 1,
'profil.private.situation' : 1,
'profil.private.skype' : 1,
'profil.facebook' : 1
}
for i in contact_confidentiality["profil"]["private"]["confidential"]:
i_json = self._doc_to_json(i)
logging.info(i_json)
del projection[i_json] # I need a key like "profil.nationality" but my key is "nationality"
contact = db.cara.users.find_one({"_id" : contact_id}, projection)
Currently, there is a limitation that you can only include or exclude, but not both in a projection. So if you are going to pass a projection, they need to either be all 1's or all 0's.
There is one exception to this which is when you are using covered indexes, you can exclude the _id field, and include other fields.
There is currently a ticket requesting that you be able to mix include and exclude, but it is not implemented yet. Check out http://jira.mongodb.org/browse/SERVER-391
To solve the specific problem in the query above, I suggest you take a different approach to your query... Instead of creating a projection by setting 1's and 0's, you can simply build a projection that only includes the things you want and leaves out the others. For example, something like this:
user_id = ObjectId(self.get_argument("id"))
contact_id = ObjectId(self.get_argument("contactId"))
contact_confidentiality = db.cara.users.find_one({"_id" : contact_id}, {"profil.private.confidential" : 1})
projection = {
'profil' : {
'private' : {
'first_name': 1,
'last_name':1,
'gender':1,
'dob' : 1,
'email' : 1,
'nationality': 1,
'work' : 1,
'situation' : 1,
'skype' : 1,
'facebook' : 1
}
}
}
for i in contact_confidentiality["profil"]["private"]["confidential"]:
projection['profil']['private'].remove(i)
contact = db.cara.users.find_one({"_id" : contact_id}, projection)
精彩评论