Consider a report that contains:
{
"name": "n1",
"version": "1.0",
"ids": ["ABC", "XYZ"]
}
I want to find all reports that contain that name, version and ids. So I built:
.find({ "name": "n1", "version": "1.0", "ids": ["ABC", "XYZ"]})
But the problem is that I don't know the order of the elements in the ids array. So the following query won't return reports:
.find({ "name": "n1", "version": "1.0", "ids": ["XYZ","ABC"]})
How can I tell the query to match if the array contains exactly does elements? Using MongoDB 3.2 and Pymongo.
You need to combine $size with $all, like so:
The $all operator selects the documents where the value of a field is
an array that contains all the specified elements
The $size operator matches any array with the number of elements
specified by the argument
{
"cast": {
$all: ['Peter Courtney', 'James J. Corbett'],
$size : 2
}
}
But be careful because it can be a very expensive operation.
You can use $all and $and operators, not the best and would need to determine size of array upfront
.find( { "name": "n1", "version": "1.0" , "ids" : { $all: ["XYZ","ABC"] }, "ids": { $size: 2 }})
I think this should work on 3.2.
Related
Hi I am trying to increment the count of the matching requirement in an array. My sample collection looks like the following:
{
"_id": ObjectId("60760ba2e870fa518f2ae48b"),
"userId": "6075f7289822d94dca8066b4",
"requirements": [
{
"searchText": "zee5",
"planType": "basic",
"mode": "PRIVATE",
"count": 32.0
},
{
"searchText": "sony",
"planType": "standard",
"mode": "PUBLIC",
"count": 12.0
},
{
"searchText": "prime",
"planType": "premium",
"mode": "PRIVATE",
"count": 2
}
]
}
If a user searches for prime, with filter premium and PRIVATE, then the count of the last requirement should be updated. If he searches for prime, with filter standard and PRIVATE, then the new requirement will be inserted with count 1.
I am doing in two steps. First I fire an update with the following query and then if no update, I fire a push query with count 1:
db.getCollection('userProfile').update({ "$and" : [{ "requirements.searchText" : {$eq:"prime"}}, {"requirements.mode" : {$eq: "PUBLIC"}}, {"requirements.planType": {$eq: "standard"}}, { "userId" : "6075f7289822d94dca8066b4"}]}, {$inc: {"requirements.$.count" : 1}})
I was expecting that the above query will not update any requirement, since there is no exact match. Interestingly, it increments the count of the second requirement with (sony, standard, public). What is wrong with the query? How can I get it right?
Demo - with Update - https://mongoplayground.net/p/-ISXaAayxxv
Demo No update - https://mongoplayground.net/p/88bTj3lz7U_
Use $elemMatch to make sure all properties are present in the same object inside the array
The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.
db.collection.update(
{
"requirements": {
$elemMatch: { "searchText": "prime","mode": "PUBLIC", "planType": "standard" }
},
"userId": "6075f7289822d94dca8066b4"
},
{ $inc: { "requirements.$.count": 1 } }
)
Problem -
Your current query will match any document with all these fields in
requirements array in any object, if they match 1 property in 1 index of the array and another match in the next index query will find the document valid.
"searchText": "prime",
"mode": "PUBLIC",
"planType": "standard"
I am beginner in MongoDB and struck at a place I am trying to fetch data from nested array but is it taking so long time as data is around 50K data, also it is not much accurate data, below is schema structure please see once -
{
"_id": {
"$oid": "6001df3312ac8b33c9d26b86"
},
"City": "Los Angeles",
"State":"California",
"Details": [
{
"Name": "Shawn",
"age": "55",
"Gender": "Male",
"profession": " A science teacher with STEM",
"inDate": "2021-01-15 23:12:17",
"Cars": [
"BMW","Ford","Opel"
],
"language": "English"
},
{
"Name": "Nicole",
"age": "21",
"Gender": "Female",
"profession": "Law student",
"inDate": "2021-01-16 13:45:00",
"Cars": [
"Opel"
],
"language": "English"
}
],
"date": "2021-01-16"
}
Here I am trying to filter date with date and Details.Cars like
db.getCollection('news').find({"Details.Cars":"BMW","date":"2021-01-16"}
it is returning details of other persons too which do not have cars- BMW , Only trying to display details of person like - Shawn which have BMW or special array value and date too not - Nicole, rest should not appear but is it not happening.
Any help is appreciated. :)
A combination of $match on the top-level fields and $filter on the array elements will do what you seek.
db.foo.aggregate([
{$match: {"date":"2021-01-16"}}
,{$addFields: {"Details": {$filter: {
input: "$Details",
as: "zz",
cond: { $in: ['BMW','$$zz.Cars'] }
}}
}}
,{$match: {$expr: { $gt:[{$size:"$Details"},0] } }}
]);
Notes:
$unwind is overly expensive for what is needed here and it likely means "reassembling" the data shape later.
We use $addFields where the new field to add (Details) already exists. This effectively means "overwrite in place" and is a common idiom when filtering an array.
The second $match will eliminate docs where the date matches but not a single entry in Details.Cars is a BMW i.e. the array has been filtered down to zero length. Sometimes you want to know this info so if this is the case, do not add the final $match.
I recommend you look into using real dates i.e. ISODate instead of strings so that you can easily take advantage of MongoDB date math and date formatting functions.
Is a common mistake think that find({nested.array:value}) will return only the nested object but actually, this query return the whole object which has a nested object with desired value.
The query is returning the whole document where value BMW exists in the array Details.Cars. So, Nicole is returned too.
To solve this problem:
To get multiple elements that match the criteria you can do an aggregation stage using $unwind to separate the different objects into array and match by the criteria you want.
db.collection.aggregate([
{
"$match": { "Details.Cars": "BMW", "date": "2021-01-26" }
},
{
"$unwind": "$Details"
},
{
"$match": { "Details.Cars": "BMW" }
}
])
This query first match by the criteria to avoid $unwind over all collection.
Then $unwind to get every document and $match again to get only the documents you want.
Example here
To get only one element (for example, if you match by _id and its unique) you can use $elemMatch in this way:
db.collection.find({
"Details.Cars": "BMW",
"date": "2021-01-16"
},
{
"Details": {
"$elemMatch": {
"Cars": "BMW"
}
}
})
Example here
You can use $elemenMatch into query or projection stage. Docs here and here
Using $elemMatch into query the way is this:
db.collection.find({
"Details": {
"$elemMatch": {
"Cars": "BMW"
}
},
"date": "2021-01-16"
},
{
"Details.$": 1
})
Example here
The result is the same. In the second case you are using positional operator to return, as docs says:
The first element that matches the query condition on the array.
That is, the first element where "Cars": "BMW".
You can choose the way you want.
So, I have the following structure in some documents:
{
"Group": [
{
"data": {
"field1": "VALUE1",
"otherfield": "XXXX"
}
},
{
"data": {
"field1": "VALUE2",
"otherfield": "YYYYY"
}
}
]
}
The size of the Group array can be either 0, 1 or 2 in size. What I need to to is match the documents which contains both of VALUE1 and VALUE2 for field1. Couldn't find a suitable answer in here for this specific case.
I tried using $elemMatch but it will not work to bring only documents with both values. That is, it will work like an or not and.
You can use the $all array query operator with dot notation for this:
db.test.find({'Group.data.field1': {$all: ['VALUE1', 'VALUE2']}})
The $all operator selects the documents where the value of a field is an array that contains all the specified elements.
I have mongodb document with data as following :
[{
"name": "Robin singh",
"developer": "java",
"address": "Robin Singh,Mohali",
"search_string": ["Robin", "Singh", "java"]
}, {
"name": "Rohan singh",
"developer": "java",
"address": "Rohan Singh,Mohali",
"search_string": ["Rohan", "Singh", "java"]
}]
I want to search document with developer java with name Rohan singh and I used this query:
{"search_string":{"$all":["Robin","Singh","java"]}}
but I am getting both results.
If you have both name and developer then you can simply use find query -
db.collection.find({"name":"Rohan singh", "developer":"java"})
Additionally, If you want to check when search_string contains exact parameters which you are passing in $all then - You need to use combination of $size and $all operator to get desired result. size must be the number of parameters that you are using in $all. Here you must check size of search_string to number of params in $all so that it will search given parameters ($all) only in those search_string which size matches to number of params.
Following query might be helpful to you.
db.collection.find({
"name":"Rohan singh",
"search_string": {
$all: ["Rohan", "singh","java"]
},
"search_string": {
$size: 3 // This is the number of param you pass in $all
}
}).pretty()
Try:
db.testcollection.aggregate([
{ $match: {"search_string":{"$all":["Robin","Singh","java"]}} },
{ $group: { _id: "$name"} }
])
Is it possible to query mongodb to return array of matching document id values, without the related keys?
Please consider following 'parent' data structur:
{
"_id": ObjectId("52448e4697fb2b775cb5c3a7"),
"name": "Peter",
"children": [
{
"name": "joe"
}
]
},
{
"_id": ObjectId("52448e4697fb2b775cb5c3b6"),
"name": "Marry",
"children": [
{
"name": "joe"
}
]
}
I would to query for an array of parent _ids whose children have the name "joe"
For provided sample data, I would like the following output returned from mongo:
[ObjectId("52448e4697fb2b775cb5c3a7"), ObjectId("52448e4697fb2b775cb5c3b6")]
I know that I can query for an output like this, which also contains the keys
[{"_id": ObjectId("52448e4697fb2b775cb5c3a7")}, {"_id": ObjectId("52448e4697fb2b775cb5c3b6")}]
However I need to push above array to another document with an update operation like this:
db.statistic.update({"date": today}, {$push: {"children": [ObjectId("52448e4697fb2b775cb5c3a7"), ObjectId("52448e4697fb2b775cb5c3b6")]}}, true, false)
I would like to avoid sorting out the document structure, in case it is possible to just return an array containing the appropriate values using mongo
It should be possible by
db.coll.distinct("_id", {"children.name": "joe"})