-1

So the issue is I have nested objects in a and array that no longer exist in the related collection. The collection (users) searched through looks like

{
    "_id" : id,
    "locations" : [
            {
                    "_id" : id,
                    "name" : name
            }
    ] 
}

The collection that the array relates to (locations) looks like.

{
        "_id" : id,
        "name" : name, ..........
}

So I'm running a map function to get all id and name in a map to reduce the fields to what I want to work with

    vals = db.locations.find({},{
       name : 1,_id : 1}).map(function(a){
          return {
            name : a.name,_id : a._id
          };
       })

Next I want to run an update which is something like this

    db.users.update({
       "locations" : {
         "name" : {$nin : vals.name},
         "_id" : {$nin : vals._id}}}, { 
             "$pull" : { 
               "array" : {
                 "name" : {$nin : vals.name},
                 "_id" : {$nin : vals._id }}}},
                 { upsert: false, multi: true 
               })

But I'm not sure how to do this or if it's even possible.

Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
Kuma
  • 1
  • 1
  • Your repeated use of 'id' and 'name' make it unclear what you are asking. Would you please expand your example, preferably with unique identifers (ie ids of 1, 2, 3 and names of a, b, c) for clarity? Did you try the code you suggested? What happened? – betseyb Apr 20 '15 at 19:47

1 Answers1

0

what you should be able to do is

vals = db.locations.find({},{name : 1,_id : 1}).map(function(a){return {name : a.name,_id : a._id};})

vals.forEach(function(x){
    db.locations.update({ _id: x._id, ...});
});

The _id must stay the same however.

Dominic Scanlan
  • 1,009
  • 1
  • 6
  • 12
  • Note though that the first on is getting a map of the locations and the second update is on the first data set (users data set) not the location data set. – Kuma Apr 20 '15 at 20:53