I have a collection in mongoDB with documents like:
{
"_id" : ObjectId("some_id"),
"name" : "name",
"field1" : 123,
"field2" : 234,
"arr" : [
{
"a_num" : 3,
"arr" : [
"abc",
"def"
],
},
{
"a_num" : 36,
"arr" : [
"hi"
],
},
{
"a_num" : 34,
"arr" : [
"abc"
],
}
]
}
While I'm using find()
I don't want to get the arr elements where arr has only 1 element, that its value is "abc".
E.g, for the above document, I'd like to get:
{
"_id" : ObjectId("some_id"),
"name" : "name",
"field1" : 123,
"field2" : 234,
"arr" : [
{
"a_num" : 3,
"arr" : [
"abc",
"def"
],
},
{
"a_num" : 36,
"arr" : [
"hi"
],
},
]
}
Any idea how?
Thanks!
You'll need to use the aggregation framework with $filter, like so:
db.collection.aggregate([
{
$addFields: {
arr: {
$filter: {
input: "$arr",
cond: {
$ne: [
"$$this.arr",
[
"abc"
]
]
}
}
}
}
}
])
I have the following collection:
{ "_id" : 1, "tags" : [ "school", "home" ] }
{ "_id" : 2, "tags" : [ "school", "storage", "home" ] }
{ "_id" : 3, "tags" : [ "school", "storage", "home", "bones" ] }
{ "_id" : 4, "tags" : [ "school", "storage", "home", "bones", "milk" ] }
{ "_id" : 5, "tags" : [ "homes", "maps", "bones" ] }
{ "_id" : 6, "tags" : [ "xxx", "yyy", "zzz" ] }
I would like to return all documents where "tags" matches documents with exactly the same array elements.
So thus, if I were to query for tags == ["school", "home"] or tags == ["home", "school"], document w/ _id=1 would be returned
I am doing the following:
db.test.find({tags: {$in: ['school', 'home']}}) or
db.test.find({tags: {$in: ['home', 'school']}})
and the following are returned:
{ "_id" : 1, "tags" : [ "school", "home" ] }
{ "_id" : 2, "tags" : [ "school", "storage", "home" ] }
{ "_id" : 3, "tags" : [ "school", "storage", "home", "bones" ] }
{ "_id" : 4, "tags" : [ "school", "storage", "home", "bones", "milk" ] }
How may I achieve only item w/ _id=1 be returned?
Maybe something like this:
Option 1:
db.collection.find({
$and: [
{
tags: "school"
},
{
tags: "home"
},
{
tags: {
$size: 2
}
}
]
})
Explained:
Match if school & home are in the tags array and if array size is 2.
Playground
Option 2:
db.collection.find({
$or: [
{
tags: [
"school",
"home"
]
},
{
tags: [
"home",
"school"
]
}
]
})
Explained:
Match the two possible array options
Playground2
i am new to MongoDB and I have documents as below
{ "_id" : ObjectId("604b7d62b19a72a2b89028e6"), "name" : "ram", "tags" : [ "mobile", "iphone", "india" ] }
{"_id" : ObjectId("604b7d83b19a72a2b89028e7"), "name" : "shyam", "tags" : [ "mobile", "iphone", "india" ] }
{ "_id" : ObjectId("604b7d9bb19a72a2b89028e8"), "name" : "ravi", "tags" : [ "mobile", "android", "india" ] }
{ "_id" : ObjectId("604b7db5b19a72a2b89028e9"), "name" : "aman", "tags" : [ "mobile", "android", "india" ] }
{ "_id" : ObjectId("604b7db5b19a72a2b89028e9"), "name" : "aman", "tags" : [ "windows", "usa" ] }
{ "_id" : ObjectId("604b7db5b19a72a2b89028e9"), "name" : "aman", "tags" : [ "tech", "apple", "microsoft" ] }
How to write query so that if i query for following tags ["mobile", "android", "12", "pro"] i would get following result
{ "_id" : ObjectId("604b7d9bb19a72a2b89028e8"), "name" : "ravi", "tags" : [ "mobile", "android", "india" ] }
{ "_id" : ObjectId("604b7db5b19a72a2b89028e9"), "name" : "aman", "tags" : [ "mobile", "android", "india" ] }
{ "_id" : ObjectId("604b7d62b19a72a2b89028e6"), "name" : "ram", "tags" : [ "mobile", "iphone", "india" ] }
{ "_id" : ObjectId("604b7d83b19a72a2b89028e7"), "name" : "shyam", "tags" : [ "mobile", "iphone", "india" ] }
Demo - https://mongoplayground.net/p/EMW8xgV1yrU
Use $in
The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype:
db.collection.find({
tags: { $in: [ "mobile", "android", "12", "pro" ] }
})
Here is the information collection and sample data:
Query to get all docs with eid:abc and sorted by role.
db.information.find({eid:"abc"}).sort({role:1}) giving expected
result, but when changing eid:aaa, result is not coming in sorting
order, order is random.
On contradictory when changing sort object to role:-1, its sorting
output in ascending order but then for eid:abc output comes in
descending order. it's just behaving strangely.
Using MongoDB shell version v4.0.6 :
information =[
{_id:'110',role:'dev',eid:'aaa',info:["a","b"]},
{_id:'111',role:'tester',eid:'abc',info:["a","b","c"]},
{_id:'112',role:'admin',eid:'abc',info:["a","c"]}
{_id:'113',role:'admin',eid:'abc',info:["a","b","c"]},
{_id:'114',role:'dev',eid:'abc',info:["a","b","c"]},
{_id:'115',role:'admin',eid:'aaa',info:["a","b","c"]}
];
Output:
1.when .find({eid:"aaa"}).sort:{role:1}
=>
[
{_id:'115',role:'admin',eid:'aaa',info:["a","b","c"]}
{_id:'110',role:'dev',eid:'aaa',info:["a","b"]},
];
2.when .find({eid:"abc"}).sort:{role:1}
=> [
{_id:'111',role:'tester',eid:'abc',info:["a","b","c"]},
{_id:'114',role:'dev',eid:'abc',info:["a","b","c"]}
{_id:'112',role:'admin',eid:'abc',info:["a","c"]}
{_id:'113',role:'admin',eid:'abc',info:["a","b","c"]}];
3.when .find({eid:"aaa"}).sort:{role:-1}
=>
[{_id:'110',role:'dev',eid:'aaa',info:["a","b"]}
{_id:'115',role:'admin',eid:'aaa',info:["a","b","c"]}]
4.when .find({eid:"abc"}).sort:{role:-1}
=>
[{_id:'113',role:'admin',eid:'abc',info:["a","b","c"]},
{_id:'112',role:'admin',eid:'abc',info:["a","c"]},
{_id:'111',role:'tester',eid:'abc',info:["a","b","c"]},
{_id:'114',role:'dev',eid:'abc',info:["a","b","c"]}]
This is what I got with a similar environment using Mongo Shell. The sort works fine for all four cases.
Note that the find query filter is on the field eid and the sort is applied on the field role.
> db.sorts.find()
{ "_id" : "110", "role" : "dev", "eid" : "aaa", "info" : [ "a", "b" ] }
{ "_id" : "111", "role" : "tester", "eid" : "abc", "info" : [ "a", "b", "c" ] }
{ "_id" : "112", "role" : "admin", "eid" : "abc", "info" : [ "a", "c" ] }
{ "_id" : "113", "role" : "admin", "eid" : "abc", "info" : [ "a", "b", "c" ] }
{ "_id" : "114", "role" : "dev", "eid" : "abc", "info" : [ "a", "b", "c" ] }
{ "_id" : "115", "role" : "admin", "eid" : "aaa", "info" : [ "a", "b", "c" ] }
> db.sorts.find({ eid: "aaa" }).sort({ role: 1 })
{ "_id" : "115", "role" : "admin", "eid" : "aaa", "info" : [ "a", "b", "c" ] }
{ "_id" : "110", "role" : "dev", "eid" : "aaa", "info" : [ "a", "b" ] }
> db.sorts.find({ eid: "abc" }).sort({ role: 1 })
{ "_id" : "112", "role" : "admin", "eid" : "abc", "info" : [ "a", "c" ] }
{ "_id" : "113", "role" : "admin", "eid" : "abc", "info" : [ "a", "b", "c" ] }
{ "_id" : "114", "role" : "dev", "eid" : "abc", "info" : [ "a", "b", "c" ] }
{ "_id" : "111", "role" : "tester", "eid" : "abc", "info" : [ "a", "b", "c" ] }
> db.sorts.find({ eid: "aaa" }).sort({ role: -1 })
{ "_id" : "110", "role" : "dev", "eid" : "aaa", "info" : [ "a", "b" ] }
{ "_id" : "115", "role" : "admin", "eid" : "aaa", "info" : [ "a", "b", "c" ] }
> db.sorts.find({ eid:"abc" }).sort({ role: -1 })
{ "_id" : "111", "role" : "tester", "eid" : "abc", "info" : [ "a", "b", "c" ] }
{ "_id" : "114", "role" : "dev", "eid" : "abc", "info" : [ "a", "b", "c" ] }
{ "_id" : "112", "role" : "admin", "eid" : "abc", "info" : [ "a", "c" ] }
{ "_id" : "113", "role" : "admin", "eid" : "abc", "info" : [ "a", "b", "c" ] }
I have the following collection
>db.prueba.find({})
{ "_id" : "A", "requi" : null }
{ "_id" : "D", "requi" : [ "A", "B" ] }
{ "_id" : "E", "requi" : [ "B" ] }
{ "_id" : "B", "requi" : null }
{ "_id" : "C", "requi" : [ "A" ] }
I need each element of the requi field to be in the following array. in this case, the array has only one element
['A']
When I use the operator $elemMatch returns the following
db.prueba.find({requi:{$elemMatch:{$in:['A']}}})
{ "_id" : "D", "requi" : [ "A", "B" ] }
{ "_id" : "C", "requi" : [ "A" ] }
the query must returns only document
{ "_id" : "C", "requi" : [ "A" ] }
please, help me
Use $eq operator to find the exact match in the array
db.collection.find({ "requi": { "$eq": [ "A" ] } })
Output
[
{
"_id": "C",
"requi": [
"A"
]
}
]
Check it here