2

i have an embedded array of tweets in my Mongodb and with every tweet comes a tweetID (not to be confused with the mongodb assigned id).

{
"_id": {
    "$oid": "54d95cb062917897ad26c5e5"
},
"name": "foo",
"tweets": [
    {
        "tweetID": "1234",
        "text": "bla bla bla"
    },
    {
        "tweetID": "4321",
        "text": "bla bla bla",
    }

... and so on

Now in my app I have another array full of tweetID's that should be deleted.

var tweetsToBeDeleted = [ "1234", "4321" ];

And i am trying to get mongoose to do so with:

ChannelsModel.update( { },
                      { $pull: { tweets: { $elemMatch: { tweetID: tweetsToBeDeleted } } } },
                      { multi: true },
                      function ( err ) {
                         console.log( "err - " + err );
                      } 
);

But when i try to run this, nothing gets deleted. The error callback will executed but err is null.

Any ideas?

Thanks in advance!

1 Answer 1

3

Use $in when matching a field to multiple values:

var tweetsToBeDeleted = [ "1234", "4321" ];
ChannelsModel.update( { },
                      { $pull: { tweets: { tweetID: { $in: tweetsToBeDeleted } } } },
                      { multi: true },
                      function ( err ) {
                         console.log( "err - " + err );
                      } 
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.