1

I'm having a (hopefully) small syntax problem with $pull in Mongodb.

bulk.find({_id: new mongo.ObjectID(req.session._id)}).updateOne({$pull: {
  firstArray: {id: req.params.id},
  'secondArray.firstArrayIds': req.params.id
  'secondArray.$.firstArrayIds': req.params.id
}});

The firstArray $pull works just fine. But the secondArray.firstArrayIds and/or secondArray.$.firstArrayIds does not. What am I doing wrong here?

This is my data structure:

clients: {
  firstArray: [
    {
      _id: '153'.
      someField1: 'someVal1',
    }
    ...
  ]

  secondArray: [
    {
      _id: '7423'.
      someField1: 'someVal1',
      firstArrayIds: [153, 154, 155, ...]
    }
    ...
  ]
}

EDIT What if there are more than one embedded object in secondArray which firstArrayIds can contain the id i want to delete. In other words, when deleting an object in firstdArray i want to delete references in all secondArray's firstArrayIds Not just the first match.

zhulien
  • 5,145
  • 3
  • 22
  • 36
Anders Östman
  • 3,702
  • 4
  • 26
  • 48

1 Answers1

2

Your "secondArray" has a nested element structure, so you must identify the outer element you want to match in your query when using a positional $ operator in the update. You basically need something like this:

bulk.find({ 
    "_id": new mongo.ObjectID(req.session._id), 
    "secondArray._id": "7423" 
}).update({
    "$pull": { 
        "firstArray": { "_id": "153" },
        "secondArray.$.firstArrayIds": 153
    }
});

So there are in fact "two" id values you need to pass in with your request in addition to the general document id. Even though this is nested it is okay since you are only matching on the "outer" level and only on one array. If you tried to match the position on more than one array then this is not possible with the positional operator.

Anders Östman
  • 3,702
  • 4
  • 26
  • 48
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • I edited my question regarding the topic I know you have been touching in your answers. I have been searching for ways to affect more than one array, and it seems that this is not possible right now. http://stackoverflow.com/questions/17765077/pull-multiple-object-in-mongo-does-not-work Or is there a way? – Anders Östman Oct 03 '14 at 12:05
  • @AndersÖstman Depends on what you mean by more than one array. As I pointed to and also mentioned in that link, is that you cannot "match the position" in more than one array or in anything other than a "top level" array. But you can always use the "same" matched position, which is often a cheat. If you have a specific case then you can always post another question. Probably best formed as looking for a solution to what you are trying to achieve. This form of update works, and a general workaround for "more than one" is multiple operations in bulk. But another question makes the case clear. – Neil Lunn Oct 03 '14 at 12:16
  • A new question is up: http://stackoverflow.com/questions/26179021/pull-object-from-array-and-pull-references-in-another-array – Anders Östman Oct 03 '14 at 12:35
  • @AndersÖstman before I comment there, that looks like very much the same question and therefore has the same answer. Is there some reason that you think the above does not work? – Neil Lunn Oct 03 '14 at 12:42
  • I'm sorry. I don't know how to express myself in another way. When you reference "the above" in your previous comment. Do you mean your answer, or do you mean comment starting with: "Depends on...", that maybe gives me an answer i dont understand. =P – Anders Östman Oct 03 '14 at 12:49
  • @AndersÖstman Doesn't matter. Your new question explains itself well enough on a further reading. – Neil Lunn Oct 03 '14 at 13:11