--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/9f732ff3-971b-44f0-bd2c-bb8ceb0253c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I will be given an array of strings, and I need to find the first one that doesn’t exist as a property in any document in a collection.
Hi Harley,
Have you tried what Oleg has mentioned above ? The $nin operator seems to be what you’re looking for. For example, given below example documents:
{ "_id" : 1, "ip" : "172.16.0.1", "name" : "cname" }
{ "_id" : 2, "ip" : "172.16.0.2", "name" : "piuuf" }
{ "_id" : 3, "ip" : "172.16.0.3", "name" : "tbooe" }
{ "_id" : 4, "ip" : "172.16.0.4", "name" : "gleap" }
Given the array ["172.16.0.1", "172.16.0.2", "172.16.0.4", "172.16.0.5"], you can use the below query to find "172.16.0.3":
db.collection.findOne({"ip":{
"$nin":["172.16.0.1", "172.16.0.2", "172.16.0.4", "172.16.0.5"]
}
});
I don’t expect the number of items in the array to ever exceed ~2000, and it isn’t a query that will run often
Depending on the size of array in $nin, you may reached the BSON Document Size limitation of 16MB while constructing the query. If that’s the case you could create a temporary collection to store the array, and use $lookup stage to find the document that doesn’t matched.
For example, store the array in a temporary collection called excluded_ip, then:
db.collection.aggregate([
{ "$lookup": {
"from":"excluded_ip",
"localField":"ip",
"foreignField":"ip",
"as":"found"}
},
{ "$match": { "found": { "$eq":[] } } }
]);
Regards,
Wan.
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/cd1e84c7-b5f2-4a1a-a38e-bd62ba8b55c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.