Assuming data in collection :
stocks:
{"scripid" : "xxxxx2" }
{"scripid" : "xxxxx3" }
{"scripid" : "xxxxx4" }
So if you need to get the list of elements from Input Array which are not in scripid of stocks collection, but not the list of elements from stocks collection which are not in input array, then use this:
db.stocks.aggregate([ {
$group :{_id : null, scripids: {$push : '$scripid'}}
},{ "$project": { _id:0 , "inputArrayNINscripts": { "$setDifference": [ ['xxxxx7','xxxxx2','xxxxx3','xxxxx8'] , "$scripids" ] } } } ])
Output:
{
"inputArrayNINscripts" : [
"xxxxx7",
"xxxxx8"
]
}
Else if you need list of elements(scripid's) from stocks scripid which aren't in passed [xxxxx7,xxxxx2,xxxxx3,xxxxx8] then, as suggested by @Caconde try this :
db.stocks.find({
"scripid": {"$nin": ["xxxxx7","xxxxx2","xxxxx3","xxxxx8"]}
}).toArray().map(scriptsNINArray => scriptsNINArray.scripid)
Output:
/* 1 */
[
"xxxxx4"
]
Add-ons:
As requested for java code, Please check these references:
mongoDB-java-driver Aggregation , SO link to java aggregation example
db.stocks.find({ '$expr': { '$not': { '$in: [ "$scripid" , inputArray ] } } }).toArray().map(doc => doc.scripid)?