0

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 ?
1

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

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.