0

Please I am new in mongo db and I want to delete one item from my list, I tried several approches but it's not working.

{

name: 'ABCDE',

snapshotString: [
        '{"timestamp":1589316266621,"testObject":{}, "moreAttributes" : "XXXXX"}',
        '{"timestamp":1589316279337,"testObject":{}, "moreAttributes" : "XXXXX"}'
],
snapshots: [
   1589316266621,
    1589316279337
]
}

I want to delete a snapshotString for given timestamp I did this:

this.testCase.updateOne(
                {name:room, "snapshotString.timestamp": timestamp}, 
                { $pull: { "snapshotString.timestamp": timestamp } }, { safe: true }, 
                function(err, obj) {
                    console.log(" ***** err ***** "+err);
        });

And it's a way to have one query to delete both documents with timestamp in snapshotString and snapshots in the same query?

Thanks

3
  • Does this answer your question ? stackoverflow.com/questions/16959099/… Commented May 12, 2020 at 21:56
  • In that example snapshotString contains an array of strings, not an array of objects. Strings don't have fields, so you can't query snapshotString.timestamp - it will never match anything. You might try a regular expression for that match. Commented May 12, 2020 at 22:05
  • @whoami unfortunately not. Commented May 13, 2020 at 1:41

1 Answer 1

1

You can try the below:

this.testCase.updateOne(
    {name: room},
    {$pull:
        {snapshotString:
            {$regex: timestamp}
        }
    });

Note: I am assuming here, timestamp is a string, if not, then you should convert it to string, as $regex operator will accept only string.

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.