mongoDB PHP query where is NOT - mongodb

ok I need a quick way to do the following with mongoDB
I was looking to do a query that would search for anything that did NOT have the word Apple in the fruit or veg column in the collection
here etc
{
"fruit":"apple"
},{
"fruit":"orange"
},{
"fruit":"banana"
}

The operator you probably want to use is $nin ("not in"):
db.market.find({
'fruit': {$nin:['apple']},
'veg': {$nin:['apple']}
})
You could also use $not to negate a standard where condition.

Related

How to write a mongo query that returns fields with the expected results?

For instance I am trying to bring up the organization ids that are tagged to multiple countries in db.
db.collection.find({"Region":{$in:["CHINA","JAPAN","SOUTH_KOREA"]}})
this doesnot give me the results that they have all 3 countries in the same document. Obviously $where does not work which I can query to bring up the fields that have more than 1 country in it.
Trying this for 2 days and need your help.
Thanks in advance.
Use $all
The $all operator selects the documents where the value of a field is an array that contains all the specified elements.
db.collection.find({"Region":{ $all :["CHINA","JAPAN","SOUTH_KOREA"] } })
i hope this will go acording youre need's:
db.collection.find({
$and: [
{ Region: {$in: /\bCHINA\b/i} },
{ Region: {$in: /\bJAPAN\b/i} },
{ Region: {$in: /\bSOUTH_KOREA\b/i} }
]
})
If I'm understanding your question correctly, you are trying to match a document where the Region key is a list conataining all three countries.
db.collection.find({$and: [
{Region: {$in: ["CHINA"]}},
{Region: {$in: ["JAPAN"]}},
{Region: {$in: ["SOUTH_KOREA"]}}
])
If so, this should work for you.
These two queries worked in my case. They are as simple as they look like but somehow I missed them our after many trials I may have miswritten them. Anyways here is the solution to my question:
db.collection.find({region:{$size:3}})
db.collection.find({ "region.2":{$exists:true}})

Use $in operator to match regex in mongoengine

Following the MongoDB documentation, you can use the $in operator with a regular expression like wise db.inventory.find( { tags: { $in: [ /^be/, /^st/ ] } } ). Is there a way to achieve the same result using mongoengine?
For example pass {"tags__in": ["/^be/", "/^st/"]} to my query?
I don't think this is supported by MongoEngine in normal query construct (i.e I doubt that Doc.objects(tags__in=["/^be/", "/^st/"] will work)) but there is support for __raw__ query (https://docs.mongoengine.org/guide/querying.html#raw-queries)
so the following should work
Inventory.objects(__raw__={"tags": { "$in":["/^be/", "/^st/"]}})

How to search for a value that IS NOT equal to the speciefied value?

I am using MongoDB and I need a query to check of a value is NOT a certain value, I know how to search for a value like this:
db.getCollection('ships').find({"name": "MY BOAT"})
So lets say I need a query that finds the entries where name is not "MY BOAT"
I allready checked out the $NOT operator https://docs.mongodb.com/manual/reference/operator/query/not/
But don't understand how to apply it.
db.getCollection('ships').find( { name: { $ne: "MY BOAT" } } )
https://docs.mongodb.com/manual/reference/operator/query/ne/
follow the above url for more information
This can be accomplished with the $ne operator, so the query becomes:
db.getCollection('ships').find({"name": {$ne: "MY BOAT"}})
The credits for this answer go to #JohnnyHK

is it possible to use "$where" in mongodb aggregation functions

I need to get the length of a string value in MongoDB using aggregation functions.
it works in
db.collection_name.find({"$where":"this.app_name.length===12"})
but when implanted to
db.collection_name.aggregate({$match:
{"$where":"this.app_name.length===12"}
},
{
$group :
{
_id : 1,
app_downloads : {$sum: "$app_downloads"}
}
}
);
I got this result:
failed: exception: $where is not allowed inside of a $match aggregation expression
The question is: is it possible to use $where in aggregation functions?
or is there any way of getting the length of a string value in aggregation function?
Thanks in advance
Eric
MongoDB doesn't support $where in aggregation pipeline and hope this will never happen, because JavaScript slows things down. Never the less, you still have options:
1) Мaintain additional field(e.g. app_name_len) than will store app_name length and query it, when needed.
2) You can try extremely slow MapReduce framework, where you allowed to write aggregations with JavaScript.
Today I had the same problem.
Mongodb doesn't support this.app_name.length, but you can do this condition with $regex - this is not very quick, but it still works.
{"app_name": { $regex: /^.{12}$/ }}
A simple way to achieve the behaviour expected of OP would be chaining up $expr with $strLenCP
db.collection.find({
$expr: {
$eq: [
12,
{
$strLenCP: "$app_name"
}
]
}
})
Mongo Playground

Mongodb find wrong document

You can see my document as bellow. How can I write a find for my uniq fields?
I need search a document where KEYMAP is (SNUM="3151" and "SKEY"="THR" and "SID"="ID_HUT")
{
"CID":"2",
"DESCRIPTION":"test",
"SECKEY":"test",
"API":{
"SMS":"http://api.php",
"DR":"http://api2.php"
},
"LOGS":{
"IN":"log_cid_in_1",
"OUT": "log_cid_out_1"
},
"KEYMAP":[
{"SNUM":"3151","SKEY":"THR", "SID":"ID_HUT"},
{"SNUM":"3152","SKEY":"ONE", "SID":"ID_XL"},
{"SNUM":"3153","SKEY":"TWO", "SID":"ID_INDO"}
]
}
db.content_provider_map.ensureIndex({"KEYMAP.SNUM":1,"KEYMAP.SKEY":1,"KEYMAP.SID":1},{unique:true});
db.mycollection.find({"KEYMAP.SNUM":"3151","KEYMAP.SKEY":"TWO","KEYMAP.SID":"ID_XL"});# not work. it find the document
I believe you want to use $elemMatch ( http://docs.mongodb.org/manual/reference/operators/#_S_elemMatch ) here like:
find({KEYMAP: {$elemMatch: {SNUM: "3151", SKEY: "TWO", SID: "ID_XL"}}})
Also unique indexes on subdocuments do not work the way you probably think they do. They create uniqueness across all documents not just that one document. If you want a unique index on that one document then you will need to use something like $addToSet or an upsert function on the subdocument.