I want to query mongoDB field for data that is '$in' two lists (arrays) kinda:
{user: {$in: [<array1>]} AND user: {$in: [<array2>]}}
so the query returns only those 'users' that present in both arrays.
I feel this isn't right:
{user: {$in: [<array1>], [<array2>]}}
Any ideas?
Either you can use the $AND operator.
http://docs.mongodb.org/manual/reference/operator/query/and/
{'$AND':[ {'user':{$in: [<..array1..>]} },{'user':{$in: [<..array2..>] } }] }
Related
I am facing a problem with mongoose. I can't do a query filter with $nin or $ne on an array of ObjectId.
Like:
query: {
objectIds: { $nin: [ objectId1, objectId2 ] }
}
I tried to send an ObjectId.toString(), an ObjectId, but nothing works.
However, if I replace the ObjectId array by a string array or put ObjectId in it, it works.
Any idea why it doesn't work with ObjectId?
I am trying to use aggregation pipeline and $not operator to bulk update fields of matched documents, but its not working.
I know but don't want to use js loop to modify them as it is inefficient way to do that.
let result = await User.updateMany(
{_id: {$in: ids}},
[{
$set: {isActive: {$not: ["$isActive"]}}
}]
)
I find in array of object and match _id and is_active both key
ex
{
_id:'12333333333333'
name:'test',
array:[{
id:'1233449',
is_active:true
},{
id:'7987979',
is_active:false
},{
id:'9558555',
is_active:true
},{
id:'2564654',
is_active:false
}]
}
find data using mongo query
db.getCollection('demo').find({'array.id':'7987979','array.is_active':false});
not working
The reason it isnt working for you is because you are running a find operation on the array directly.
When querying for things within an array you can use $elemMatch to obtain an entire document that contains the matching array element.
If you need a custom output you can use Aggregation, with the $unwind operation in the pipeline on the array field.
Try this out on your collection and understand what it is doing
db.collectionName.aggregate([{
$unwind:"$array"
},{
$match:{
$and:[
{"array.id":'your_id'},
{"array.is_active":boolean}
]
}
}
])
My document structure is something like this
{
_id: "id1",
field1: "val1",
field2: "val2",
outcome: "ABC"
}
I have created index on outcome field. I have to find all documents with {outcome:"ABC"} or {outcome:"XYZ"} only. There is no major difference in query execution time if i use $or or $in. e.g
db.coll.find({$or:[{outcome:"ABC"},{outcome:"XYZ"}]});
db.coll.find({outcome:{$in:["ABC","XYZ"]}});
Which operator should i use $or or $in in this case? And why? Any help would be appreciated.
MongoDB docs
When using $or with <expressions> that are equality checks for the value of the same field, use the $in operator instead of the $or operator.
For example, to select all documents in the inventory collection where the quantity field value equals either 20 or 50, use the $in operator:
db.inventory.find ( { quantity: { $in: [20, 50] } } )
So in your case it is better to use
db.coll.find({outcome:{$in:["ABC","XYZ"]}});
I have a mongo collection which looks something like this:
{
title: String,
category: String
}
I want to write a query that selects various categories, similar to this:
Collection.find({category: {$in: ['Books', 'Cars', 'People']});
But I want to only select a limited number of each category, for example 5 of each book, car, people. How do I write such a query? Can I do it one query or must I use multiple ones?
You can do it using mongodb aggregation. Take a look at this pipeline:
Filter all documents by categories(using $match).
Group data by categories and create array for items with the same category(using $group and $push).
Get a subset of each array with a limited maximum length(using $project and $slice).
Try the following query:
db.collection.aggregate([
{$match: {category: {$in: ['Books', 'Cars', 'People']}}},
{$group: {_id: "$category", titles: {$push: "$title"}}},
{$project: {titles: {$slice: ["$titles", 5]}}}
])