MongoDB multiple condition in WHERE clause - mongodb

In MongoDB i have to give OR condition in WHERE clause
I am a beginner. I dont know how to acheive this?
DELETE FROM tablename WHERE id = 6 OR id =8
What is the similar query in MongoDB??

Just use the $or operator as described here.
db.tablename.remove({ $or: [ { _id: 6 }, { _id: 8 } ] })
You may also find the appropriate section in the manual for SQL comparison useful.

Related

MongoDb - Can you use $bitsAllSet in an aggregate projection or group?

I have a field stored in a mongo collection that is an integer bitmask, and I frequently use the $bitsAllSet query operator to find documents where certain bit flags are set.
Now I would like to do an aggregation to get counts of documents for each bit flag. I figured I would start by trying to project if the bit is set or not.
Something like..
db.MyCollection.aggregate(
[
{ $project: {
bit1: { $bitsAllSet: ["$myBitMask", 1] },
bit2: { $bitsAllSet: ["$myBitMask", 2] },
bit3: { $bitsAllSet: ["$myBitMask", 4] },
bit4: { $bitsAllSet: ["$myBitMask", 8] }
} }
])
but this is invalid syntax and I cannot find anything in the mongo documentation that looks like it would help me accomplish this.
Is there a way achieve this?

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}})

MongoDB: distinct tuples

Suppose to have a collection of MongoDB documents with the following structure:
{
id_str: "some_value",
text: "some_text",
some_field: "some_other_value"
}
I would like to filter such documents so as to obtain the ones with distinct text values.
I learned from the MongoDB documentation how to extract unique field values from a collection, using the distinct operation. Thus, by performing the following query:
db.myCollection.distinct("text")
I would obtain an array containing the distinct text values:
["first_distinct_text", "second_distinct_text",...]
However, this is not the result that i would like to obtain. Instead, I would like to have the following:
{ "id_str": "a_sample_of_id_having_first_distinct_text",
"text": "first_distinct_text"}
{ "id_str": "a_sample_of_id_having_second_distinct_text",
"text": "second_distinct_text"}
I am not sure if this can be done with a single query.
I found a similar question which, however, do not solve fully my problem.
Do you have any hint on how to solve this problem?
Thanks.
You should look into making an aggregate query using the $group stage, and probably using the $first operator.
Maybe something along the lines of:
db.myCollection.aggregate([{ $group : { _id : { text: "$text"},
text: { $first: "$id_str" }
}
}])
try:
db.myCollection.aggregate({$group: {_id: {'text': "$text", 'id_str': '$id_str'}}})
More information here: http://docs.mongodb.org/manual/reference/method/db.collection.aggregate/

How to translate this Mongodb clause into mongoengine clause?

db.students.find( { grades: { $elemMatch: {
mean: { $gt: 70 },
grade: { $gt:90 }
} } },
{ "grades.$": 1 } )
this is on official mongodb document, I have troubling translate them into mongoengine clause.
Can anybody translate this mongodb clause into mongoengine clause?
Because I have a embedded document in an array, I need to filter something inside the embedded document, I found this on the document, but don't know the exactly way to translate it. thank you!
I got the answer, but I do it another way around, I implement elasticsearch into my mongodb to be the search engine, so that's not the problem

Mongodb $and returns no results

This is a similar question to $and query returns no result, however the answer there does not apply in this case.
I have the following query:
{
$and: [
{
ownerId: "505b832c975a5c3ca6e9523b"
},
{
privacyLevel: "Public"
}
]
}
My collection has 16 documents, all of which are "Public" and 7 of which have the ownerId above. The subqueries behave correctly and return the correct documents so I would expect 7 results from this query.
The $and query returns nothing, I am at a loss as to why that might be.
If you are just querying two fields you do not need an $and operator. Your query will simply be:
.find({ownerId: "505b832c975a5c3ca6e9523b", privacyLevel: "Public"})