Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Web Technology by (50.2k points)

The following is my document inside my MongoDB collection:

    title:"Happy thanksgiving",

    body: "come over for dinner",

    blocked:[

       {user:333, name:'john'},

       {user:994, name:'jessica'},

       {user:11, name: 'matt'},

    ]

}

How can we find all documents that do not have user 11 in a sub-document named "blocked"?

1 Answer

0 votes
by (108k points)

You can apply $in or $nin to your queries if you want to use "not in". In MongoDB, you can also use the NOT EQUALS operator, $ne, to get what you want as it uses the value that cannot be searched, refer to the following command:

db.myCollection.find({'blocked.user': {$ne: 11}});

If you want multiple values then use $nin:

db.myCollection.find({'blocked.user': {$nin: [11, 12, 13]}});

Related questions

0 votes
2 answers
asked Sep 4, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
0 votes
2 answers

Browse Categories

...