Composition in mongo query (equivalent to SQL query)

I have several collections, for example:

members
   id
   name
   //other fields we don't care about
emails
   memberid
   //other fields we don't care about

I want to delete the email address for this member. In SQL, I could use a nested query, for example:

delete emails
where memberid in (select id from members where name = "evanmcdonnal")

In mongo, I try something like this;

db.emails.remove( {"memberid":db.members.find( {"name":"evanmcdonnal"}, {id:1, _id:0} ) )

But it does not return results. Therefore, I took a subquery and ran it myself. I believe that it returns:

{
    "id":"myMadeUpId"
}

What - if internal queries are executed first - asks me a request:

db.emails.remove( {"memberid":{ "id""myMadeUpId"} )

When I really need an id value. I tried using dictionary and dot notations to access the id value without any luck. Is there a way to do this, similar to my requested request above?

+4
source share
3 answers

,

, memberid in ( id , name = "evanmcdonnal" )

. :

db.members.find({ "name" : "evanmcdonnal" }, { "id" : 1 }).forEach(function(doc) {
    db.emails.remove({ "memberid" : doc.id });
});

members. members id $in:

var plzDeleteIds = db.members.find({ "name" : "evanmcdonnal" }, { "id" : 1 }).toArray();
db.emails.remove({ "memberid" : { "$in" : plzDeleteIds } });

, plzDeleteIds , . . , , MongoDB ( 2.6, ).

MongoDB - , , , , . , , , , -, .

+10

Each(), , , :

var plzDeleteIds = db.members.find({ "name" : "evanmcdonnal" }, { "id" : 1 }).toArray();
var aux = plzDeleteIds["0"];
var aux2 = aux.map(function(u) { return u.name; } );
db.emails.remove({ "memberid" : { "$in" : aux2 } });

, !

+1

, , , . MongoDB - . , : . "emails", , . . , , . , , : db.emails.remove({emailAddress: theActualAddress))

?

0

All Articles