Gurus – I’m stuck in a situation that I can’t figure out how I can query from the following collection “spouse”, which has embedded document “surname” and check for equality with “surname” of this document:
{
"_id" : ObjectId("50bd2bb4fcfc6066b7ef090d"),
"name" : "Gwendolyn",
"surname" : "Davis",
"birthyear" : 1978,
"spouse" : {
"name" : "Dennis",
"surname" : "Evans",
"birthyear" : 1969
},
I need to query:
Output data for all spouses with the same surnames (if the surname of
one of the spouses is not specified, assume that it coincides with the
name of another)
I tried something like this:
db.task.find( {“surname” : { “spouse.surname” : 1 }} )
but it failed)
PLEASE PLEASE Guide me how I can achieve this any example/sample? based on this will be really helpful 🙂
Thanks a lot!
You have three options.
Use
$wheremodifier:db.task.find({$where: 'this.spouse.surname === this.surname'})Update all your documents and add special flag. After that you will be able to query documents by this flag. It’s faster then
$where, but requires altering your data.Use MapReduce. It’s quite complicated, but it allows you to do nearly anything.