1

I have the following object in my database:

{
    "_id": "fTgR2YtHiZBzzqF6J",
    "following": [
        {
            "user": {
                "_id": "S4dLHRJiuHoyAp26q",
                "fb": {
                    "id": "100006681067911",
                    "name": "Helen Amffhajfgiaa Laubergskymanwitzescusonsteinsen"
                }
            },
            "date": "2013-10-01T17:25:50.305Z"
        },
        {
            "user": {
                "_id": "MAyxz4Yk5F9vh9RRy",
                "fb": {
                    "id": "100006719587007",
                    "name": "Mary Amfgaiehgkg Smithman"
                }
            },
            "date": "2013-10-11T10:47:58.898Z"
        }
    ]
}

Now I want to remove the

{
   "user": {
      "_id": "MAyxz4Yk5F9vh9RRy",
      "fb": {
          "id": "100006719587007",
          "name": "Mary Amfgaiehgkg Smithman"
      }
    },
    "date": "2013-10-11T10:47:58.898Z"
}

subdocument from the array called "following".

My query to do this looks like this:

 Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
 {
    "$pull": {
        "following": {
             "user": {
                 "_id": "MAyxz4Yk5F9vh9RRy"
             }
         }
     }
 });

But nothing happens!

Can someone point me towards my error?

Nils Ziehn
  • 4,118
  • 6
  • 26
  • 40

3 Answers3

4

My problem was: I had to $pull the whole object:

 Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
 {
    "$pull": {
        "following": {
            "user": {
                "_id": "MAyxz4Yk5F9vh9RRy",
                "fb": {
                    "id": "100006719587007",
                    "name": "Mary Amfgaiehgkg Smithman"
                }
            },
            "date": "2013-10-11T10:47:58.898Z"
        }
         }
     }
 });

and can NOT just call this:

Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
 {
    "$pull": {
        "following": {
             "user": {
                 "_id": "MAyxz4Yk5F9vh9RRy"
             }
         }
     }
 });
Nils Ziehn
  • 4,118
  • 6
  • 26
  • 40
0

The problem is exactly the same as https://stackoverflow.com/a/17779687/149818. You query in general right and would be work just for case if you have exactly 1 "user", but for multiple users you have to redesign your collection

Community
  • 1
  • 1
Dewfy
  • 23,277
  • 13
  • 73
  • 121
  • Hey, thx for your input. I actually found that I have a different error, because I didn't get the concept in mongo ( which I think is bad. ) I will answer my own question as soon as I am allowed to. Basically: I can't just match for the user's _id but I have to match the whole object in the array containing {_id,fb{id,name}} – Nils Ziehn Oct 14 '13 at 15:52
  • Just to find by such `_id` you need specify full path: `col.find({"following.user._id":"MAyxz4Yk5F9vh9RRy"})` - but note that `find` return record not the dictionary entry! – Dewfy Oct 14 '13 at 16:03
0

You can also try this answer (Remove Item from Array in Meteor.js), I think it's a better fit, with less code.

Community
  • 1
  • 1