Mongo Query in Nested Json Array [duplicate] - mongodb

This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 4 years ago.
A document in my collection looks like below:
{
"_id":"<uuid>",
"tests":[
{
"min":5,
"max":20
},
{
"min":15,
"max":35
}
]
}
Now, I want to find all the tests in which min >= 5.
db.coll.aggregate([
{ $match: { 'tests.min': {$gte : 5}} },
{ '$unwind': "$tests" }
]);
the above query somehow doesnt return the correct results.

You do not need to write a aggregate query for this. Use the $elemMatch operator for this. Your query will look like as shown
db.coll.find({
tests: {
$elemMatch: {
min: {
$gte: 5
}
}
}
})
The above query will return the desired result.
For more details visit
$elemMatch (query)

Related

Mongo if inside $match and $or [duplicate]

This question already has answers here:
mongo in() clause sort by most matches
(2 answers)
Closed 9 months ago.
I've been stuck with this for a quite while.
I want to filter specials.name field based on two inputs from my form - select and input field. The problem is, whatever I tried, it always chooses on of it.
Is there any way to put if, else block inside $or to make it work?
This is the last thing that I tried, and now it works always on searchInput, which is input field, it does not react on changes in select menu.
{
$match: {
$or: [
{ 'specials.name':{$in :[searchSpecials, searchInput]}},
]
}
}
const searchSpecials = req.body.searchSpecials
const searchInput = req.body.search
If the two inputs are strings, this works as expected:
db.collection.aggregate([
{
$match: {"specials.name": {$in: ["Malinda", "Joseph"]}}
}
])
and returns:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"specials": {
"name": "Malinda"
}
},
{
"_id": ObjectId("5a934e000102030405000002"),
"specials": {
"name": "Joseph"
}
}
]
As you can see here, on the playground.
No need for the $or, since the $in will return all documents that have specials.name which is included in the list of options.

How to deselect one object in array MongoDB? [duplicate]

This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 3 years ago.
In my situation I just need my result but without my objectID in my array.
This is my method :
return Room.findOne(
{
_id: idRoom,
participants: {$elemMatch: {$ne: this.currentUser.profile}},
},
{
'participants.$': 1,
}
)
With elementMatch, the problem is when you found the object, only the first object is returned.
This is my result :
"result": {
"_id": "5da5e77f51e08708b79565e8",
"participants": [
"5da4d5b40cc94f04a7aaad40"
],
And this is the real result I need
"result": {
"_id": "5da5e77f51e08708b79565e8",
"participants": [
"5da4d5b40cc94f04a7aaad40"
"fwnert9248yrhnqwid13982r" // I have another participants
],
And my model is just like this :
const RoomSchema = new Schema({
participants: [{type: Schema.Types.ObjectId,ref: 'Profile'}],
...
}, options)
For others reasons, I can't use aggregate, thank you if you have the solution
So I think you are trying to shape a resultset in mongo with the findOne() method, and any use of the aggregation pipeline framework is out of the question and unavailable for other reasons.
I am not sure this is possible. I believe you will need to perform multiple steps to achieve your desired results. If you can use aggregation pipeline framework here is a pipeline to suit the desired results (I believe)...
db.Room.aggregate(
[
{
"$match": { _id: ObjectId(idRoom)}
},
{
$project: {
"participants": {
$filter: {
input: "$participants",
as: "participant",
cond: {$ne: ["$$participant", this.currentUser.profile] }
}
}
}
}
]
)
...but if you cannot use aggregation pipeline then here is a mongoshell script that accomplishes the task in several steps. The strategy is to capture the whole document by _id then remove the data element from the array then echo the results...
var document = db.Room.findOne( { _id: ObjectId("5da64a62cd63abf99d11f210") } );
document.participants.splice(document.participants.indexOf("5da4d5b40cc94f04a7aaad40"), 1);
document;

Save length of array in extra MongoDB field [duplicate]

This question already has answers here:
MongoDB: count the number of items in an array
(3 answers)
Closed 5 years ago.
According to this answer, I am trying to find out the size of an array and save it in the extra field.
I have a collection user_details and the document structure is something like :
{
user_id : 1,
likes : [1,2,3,4],
likes_count : 0
}
The query I'm using is as follows :
db.user_details.update({user_id : 1},{$set:{ likes_count : this.likes.length }})
BUT, it throws error that
"message" : "Cannot read property 'length' of undefined"
How do I save the length of an array in extra field?
PS: I'm using MongoDB 3.4
With MongoDB 3.4 and newer, you can use the $addFields pipeline to add the desired field ($addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields) and then write the result of the aggregate operation to the same collection using $out operator, thereby effectively updating the underlying collection.
If the collection specified by the $out operation already exists, then upon completion of the aggregation, the $out stage atomically replaces the existing collection with the new results collection.
To get the count, use the $size operator which returns the length of an array field. Bear in mind that all documents must have the likes field.
Overall, you need to run the following pipeline to achieve the desired update operation:
db.user_details.aggregate([
{ "$addFields": { "likes_count": { "$size": "$likes" } } },
{ "$out": "user_details" }
])
https://docs.mongodb.com/manual/reference/operator/aggregation/size/#exp._S_size
db.users.aggregate(
[
{
$project: {
likes_count: { $size: "$test" }
}
}
]
)
store the returned likes_count in an variable and perform update by providing the likes_count variable
something like this
Model.aggregate(
[
{
$project: {
likes_count: { $size: "$test" }
}
}
], (err, re) => {
console.log(err, re);
var likes_count = re[0].likes_count;
Model.update({email: 1}, {$set: {likes_count: likes_count}}, (err, d) => {
console.log(err, d);
})
}
)

Find the count of all the matching elements in an array [duplicate]

This question already has answers here:
Mongodb count all array elements in all objects matching by criteria
(2 answers)
Closed 4 years ago.
I need to find the count of all the matching array elements and count = 0 if the nested array do not contain values.
Here is my data entity structure.
{
name:A,
issues:[1,2,3,4]
}
{
name:B,
issues:[1,2]
}
{
name:C,
issues:[3,4]
}
If user search for issues:[1,2], I want my result set to look like
[{
name:A,
count:2
}
{
name:B,
count:2
}
{
name:C,
count:0
}]
I am using below query to achieve this but it only returns me result
[{
name:A,
count:2
}
{
name:B,
count:2
}]
which definitely I know because of the $match I am doing
{'$unwind':'$issues'}
,{'$match':{'allissues': {$in: p.issues? p.issues.map(Number):[]}}}
,{ '$group' :{_id:'$_id', name :{ $first: '$name' },count: { $sum: 1 }} }'
Given that issues variable is the array input by users, you do not need to use a three-stages aggregation like above to get the result. You just need to find the intersection of users input and issues field of each document, then get length of the result arrays. This code will do the job:
db.col.aggregate([{
$project: {
count: {
$size: {
$setIntersection: [issues, "$issues"]
}
}
}
}])

Meteor - Mongo aggregate does not have $count stage [duplicate]

This question already has answers here:
MongoDB SELECT COUNT GROUP BY
(9 answers)
Closed 6 years ago.
I am using meteorhacks:aggregate package to do Mongo aggregation in Meteor. I want to get the count at the last stage of the pipeline so I use this code:
Message.aggregate([
{
$match: {
// ...
}
}, {
$count: 'count'
}
]);
It is pretty simple and should work, but I only get this error:
Exception while invoking method 'methodname'
MongoError: Unrecognized pipeline stage name: '$count'
...
Please help, thanks.
Updated: this question is not duplicated as an editor suggested, my main intention is to find out why I can not use $count
$count is available in mongodb version 3.4. For previous versions,
you will need to use $group over a constant field.
Message.aggregate([
{
$match: {
// ...
}
}, {
$group: {
_id : null,
count : {$sum : 1}
}
}
]);