Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 (107k points)
edited by

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]}});

To help you gain a better understanding, here is a Full Stack Developer Certification Course provided by Intellipaat. 

Related questions

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...