I have shops collection and user collection with list of shops ids inside of it as strings.
example of shop document:
{
"_id" : ObjectId("5a0c6797fd3eb67969316ce2"),
"picture" : "http://placehold.it/150x150",
"name" : "Genmom",
"email" : "leilaware#genmom.com",
"city" : "Rabat",
"location" : {
"type" : "Point",
"coordinates" : [
-6.79387,
33.83957
]
}
}
example of user collection:
{
"_id" : ObjectId("5c04b943ff491824b806686a"),
"email" : "ayoub.khial#gmail.com",
"password" : "$2a$10$4Wt5Rn6udxREdXCIt3hGb.sKhKUKOlyiYKmLTjYG3SqEPKFSw9phq",
"likedShops" : [
"5a0c6797fd3eb67969316ce2",
"5c07ada8ff49183284e509d1",
"5c07acc1ff49183284e509d0"
],
"dislikedShops" : [ ]
}
I want to return the detail of the likedShops.
You can use below $lookup aggregation
db.users.aggregate([
{ "$lookup": {
"from": "shops",
"let": { "likedShops": "$likedShops" },
"pipeline": [
{ "$match": { "$expr": { "$in": ["$_id", "$$likedShops"] }}}
],
"as": "likedShops"
}}
])
Or if your ids are string then use $toString aggregation with the ObjectIds
db.users.aggregate([
{ "$lookup": {
"from": "shops",
"let": { "likedShops": "$likedShops" },
"pipeline": [
{ "$match": { "$expr": { "$in": [{ "$toString": "$_id" }, "$$likedShops"] }}}
],
"as": "likedShops"
}}
])
Related
I have the below document in my collection.
{ "_id" : ObjectId("ObjectId"), "MobNo" : "23232", "data" : [ { "status" : "PASS" }, { "status" : "PASS" } ] }
{ "_id" : ObjectId("ObjectId"), "MobNo" : "232323", "data" : [ { "status" : "PASS" }, { "status" : "FAIL" } ] }
{ "_id" : ObjectId("ObjectId"), "MobNo" : "32322", "data" : [ { "status" : "PASS" }, { "status" : "PASS" } ] }
I want a query to return all objects in data array not to have PASS status(All the objects in the array should NOT have the status as "PASS").
My Expected output here is
{ "_id" : ObjectId("ObjectId"), "MobNo" : "232323", "data" : [ { "status" : "PASS" }, { "status" : "FAIL" } ] }
My query for the above result :
db.Collection.aggregate({
$match: {
"data": {
$exists: true, $not: { $size: 0 }, $all: [
{ "$elemMatch": { "status": { $nin: ["PASS"] } } },
]
},
}
})
Now i want to my collection to return documents with data array having all object status as passed .
My expected output :
{ "_id" : ObjectId("ObjectId"), "MobNo" : "23232", "data" : [ { "status" : "PASS" }, { "status" : "PASS" } ] }
{ "_id" : ObjectId("ObjectId"), "MobNo" : "32322", "data" : [ { "status" : "PASS" }, { "status" : "PASS" }
How can i get the above output, my below query doesnt work and it return even the failure data array.
db.Collection.aggregate({
$match: {
"data": {
$exists: true, $not: { $size: 0 }, $all: [
{ "$elemMatch": { "status": { $in: ["PASS"] } } },
]
},
}
})
Output of the above query :
{ "MobNo" : "23232", "data" : [ { "status" : "PASS" }, { "status" : "PASS" } ] },
{ "MobNo" : "232323", "data" : [ { "status" : "PASS" }, { "status" : "FAIL" } ] },
{ "MobNo" : "32322", "data" : [ { "status" : "PASS" }, { "status" : "PASS" } ] }
I do not wanted the below record in my above query output:
{ "MobNo" : "232323", "data" : [ { "status" : "PASS" }, { "status" : "FAIL" } ] },
Any help ?
You can do this in several different ways, here is the most straight forward in my opinion using $size and $filter to see if any "bad" documents exist in the array.
db.collection.aggregate([
{
$match: {
$expr: {
$eq: [
{
$size: {
$filter: {
input: "$data",
cond: {
$ne: [
"$$this.status",
"PASS"
]
}
}
}
},
0
]
},
"data.0": {
$exists: true
}
}
}
])
Mongo Playground
Cause your status values are in the array field (data), so you must to $unwind it, then query in it, see this:
db.Collection.aggregate({
$unwind:{ path: "$data"},
$match: {
"data": {
$exists: true, $not: { $size: 0 }, $all: [
{ "$elemMatch": { "status": { $in: ["PASS"] } } },
]
},
}
})
{
"_id" : "1",
"teams" :
[
{
"type" : "local",
"isEnabled" : "true",
"names" :
[
{ "name": "kumar","Nationality":"indian","BirthPlace":"Goa","Age":"U25" },
{ "name": "kannan","Nationality":"indian","BirthPlace":"Kerala","Age":"U25"}
]
},
{
"type" : "national",
"isEnabled" : "true",
"names" :
[
{ "name": "kumar","Nationality":"indian","BirthPlace":"Goa","Age":"U25" },
{ "name": "kannan","Nationality":"indian","BirthPlace":"Kerala","Age":"U25"}
]
},
{
"type" : "international",
"isEnabled" : "true",
"names" :
[
{ "name": "kumar","Nationality":"indian","BirthPlace":"Goa","Age":"U25" },
{ "name": "kannan","Nationality":"indian","BirthPlace":"Kerala","Age":"U25"}
]
},
]
}
I have multiple docs in the same format in a mongodb collection. I wanted to append the below lines into names array also only for "type: local" in each docs of the collections. I tried $push and its appending in all teams array as i am finding difficulty to add this condition only for "type":"local"
{
"name":"jack","Nationality":"indian","BirthPlace":" "Karnataka","Age":"U25"
}
You can use positional operator : $[]
db.collection.update({
"teams.type": "local"
},
{
$push: {
"teams.$[element].names": {
"name": "jack",
"Nationality": "indian",
"BirthPlace": "Karnataka",
"Age": "U25"
}
}
},
{
arrayFilters: [
{
"element.type": "local"
}
],
multi: true
})
Try it here
{
"attributes" : [
{
"dept" : "accounts",
"location" : "onshore"
},
{
"dept" : "HR",
"location" : "offshore"
},
{
"dept" : "technology"
"location": "NL"
}
]
},
{
"attributes" : [
{
"dept" : "accounts",
"location" : "onshore"
},
{
"dept" : "technology"
"location": "London"
}
]
},
{
"attributes" : [
{
"dept" : "accounts",
"location" : "onshore"
},
{
"dept" : "HR"
"location": "London"
}
]
}
I want to get those documents where attributes array has dept :technology with location NOTequal to london or the attributes array does not have field dept :technology. So the final output will be like below:
{
"attributes" : [
{
"dept" : "accounts",
"location" : "onshore"
},
{
"dept" : "HR",
"location" : "offshore"
},
{
"dept" : "technology"
"location": "NL"
}
]
},
{
"attributes" : [
{
"dept" : "accounts",
"location" : "onshore"
},
{
"dept" : "HR"
"location": "London"
}
]
}
I have tried this solution but it gives me all the documents:
{
"attributes": {
"$elemMatch": {
"$or": [
{
"dept": {
"$nin": ["technology"]
}
},
{
"$and": [
{
"dept": "technology"
},
{
"location": {"$ne" : "London"}
}
]
}
]
}
}
}
Since $elemMatch evaluates each item of the array, we cannot use $nin or $ne operators if the condition is only to negate the boolean expression.
Instead, we should use the $not to performs a logical NOT operation on the specified < operator-expression >.
Try this one:
db.collection.find({
"$or": [
{
"attributes": {
$not: {
"$elemMatch": {
dept: "technology"
}
}
}
},
{
"attributes": {
"$elemMatch": {
dept: "technology",
location: {
$ne: "London"
}
}
}
}
]
})
MongoPlayground
I am trying to aggregate 2 collections in MongoDB based on a student's ID. One collection consists of student personal information, another one consists of the students logs. The issue is that the data is in array which is why I think my aggregation is not working. Any help will be appreciated.
student collection
{
"_id" : ObjectId("(Object ID here"),
"data" : [
{
"name" : "John",
"id" : 1
},
{
"name" : "Sandy",
"id" : 2
}
]
}
logs collection
{
"_id" : ObjectId("(Object ID here"),
"logs" : [
{
"studentId" : 1,
"activity" : "11112,334,123"
},
{
"studentId" : 2,
"activity" : "11112,334,123"
}
]
}
Here is what I have tried:
dbo.collection("student").aggregate([
{ "$lookup": {
"localField": "data.id",
"from": "logs",
"foreignField": "logs.studentId",
"as": "studentInfo"
}
}]).toArray(function(err, results) {
console.log(results);
});
Expected result:
studentinfo: {
id: 1,
name: "John",
activity" : "11112,334,123"
}
You can use below aggregation with mongodb 3.6
So basically your foreign field is an array you need to use $lookup with the pipeline to $unwind the foreign array inside the $lookup pipeline and to match the corresponding ids.
db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}}
])
or use this to merge both the arrays
db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}},
{ "$project": {
"students": {
"$map": {
"input": "$students",
"in": {
"studentId": "$$this.studentId",
"activity": "$$this.activity",
"name": { "$arrayElemAt": ["$data.name", { "$indexOfArray": ["$data.id", "$$this.studentId"] }]}
}
}
}
}}
])
Output
[
{
"students": [
{
"activity": "11112,334,123",
"name": "John",
"studentId": 1
},
{
"activity": "11112,334,123",
"name": "Sandy",
"studentId": 2
}
]
}
]
How to realize count array in array using aggregate? I have this document structure:
{
"_id" : 1,
"link" : [
{
"linkHistory" : [
{
"_id" : 1,
},
{
"_id" : 2,
}
]
}
]
}
and MongoDB code:
db.emailGroup.aggregate([
{
"$lookup":
{
"from": "link",
"localField": "_id",
"foreignField": "emailGroupId",
"as": "link"
},
},
{
"$unwind": "$link"
},
{
"$match": {
'link.originalLink': ""
}
},
{
"$group" : {
_id: '$_id',
link: {
$push: '$link'
}
}
}
])
I want to get count in other field for linkHistory. Is this possible?