quma
quma

Reputation: 5719

MongoDBstring not in array of document

I have this MongoDB documentFile Collection entry:

db.DocumentFile.find().pretty()
{
"_id" : ObjectId("587f0d61473c92b933a68efa"),
"_class" : "com.xxx.dao.domain.DocumentFile",
"fileName" : "DocumentFile1",
"ending" : "jpg",
"projectId" : "587f0d61473c92b933a68ef9",
"active" : true,
"userIdBlackList" : [
    "587f0d61473c92b933a68ef8"
]}

And now I will find document files where some id is not in userIdBlackList. I tried this one:

db.DocumentFile.find({"‌​userIdBlackList" : { "$ne" : "587f0d61473c92b933a68ef8"}}).pretty();

But I still get the document file:

{
"_id" : ObjectId("587f0d61473c92b933a68efa"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile1",
"ending" : "jpg",
"projectId" : "587f0d61473c92b933a68ef9",
"active" : true,
"userIdBlackList" : [
    "587f0d61473c92b933a68ef8"
]}

Is there a possibility with MongoDB to say: return document when some string is not included in @Document#someArrayOfDocument?

I also have tried this queries

db.DocumentFile.find({"‌​587f0d61473c92b933a68ef8" : { "$nin" : ["587f0d61473c92b933a68ef8"]}}).pretty() -> returns the document, don't know why

db.DocumentFile.find({"‌​587f0d61473c92b933a68ef8" : { "$in" : ["587f0d61473c92b933a68ef8"]}}).pretty() -> does not return the document, also dont know why

db.DocumentFile.find({"‌​587f0d61473c92b933a68ef8" : { "$nin" : userIdBlackList}}).pretty() ->  [main] ReferenceError: userIdBlackList is not defined -> but how I can specify userIdBlackList ?

Upvotes: 0

Views: 61

Answers (3)

chridam
chridam

Reputation: 103445

You can also use $redact available in the aggregation framework as means to filter your collection:

db.DocumentFile.aggregate([
    { 
        "$redact": {             
            "$cond": [
                { 
                    "$setIsSubset": [ 
                        [ "587f0d61473c92b933a68ef8" ],  
                        "$userIdBlackList" 
                    ] 
                },
                "$$PRUNE",
                "$$KEEP"
            ]
        }
    }
])

In the above, the $redact pipeline will evaluate the userIdBlackList field by using the $setIsSubset operator which takes two arrays and returns true when the first array is a subset of the second, including when the first array equals the second array, and false otherwise.

If the array [ "587f0d61473c92b933a68ef8" ] is a subset of "$userIdBlackList" array then $redact excludes all fields at this current document/embedded document level, without further inspection of any of the excluded fields by using the $$PRUNE system variable. In effect, it acts as a match filter at document level.

Upvotes: 0

Maksim Simkin
Maksim Simkin

Reputation: 9679

What are your blacklist ids ? You query should look like:

db.DocumentFile.find({userIdBlackList: {"$nin":["blacklistId1", "blacklistId2"]}})

You could use "587f0d61473c92b933a68ef8" for example as one of your blacklistIds.

Upvotes: 1

Santanu Biswas
Santanu Biswas

Reputation: 4787

Should work using $elemMatch

db.DocumentFile.find({"‌​userIdBlackList": { $elemMatch: { $ne: "587f0d61473c92b933a68ef8" } } }).pretty();

For querying with multiple values you can use $nin

db.DocumentFile.find({"‌​userIdBlackList": { $nin: ["587f0d61473c92b933a68ef8", "587f0d61473c92b933a68ef9", ... <more values>] } }).pretty();

Hope this helps.

Upvotes: 1

Related Questions