Using a where on an array with id's inside aggregate mongodb - mongodb

Hi im really struggling finding a solution to this.
Im trying to see if an array contains a value that I give
I want all tickets of a specific organization.
The organization is saved in the user (Creator) in an array named organizations.
Models
ticket {
creator: "ObjectId"
}
user {
organizations: ["ObjectId", "ObjectId"]
}
This is what I have now
Ticket.aggregate([
{
"$lookup": {
"from": User.collection.name,
"localField": "creator",
"foreignField": "_id",
"as": "creator"
}
},
{ "$unwind": "$creator" },
{ "$match": { "creator.organizations": {
"$elemMatch": {"$in": ["60a77d5b57d8c960829a0343"]}}}
},
{ "$set": {"creator": "$creator._id"}},
])
This doesn't work tho
I read that you can't use $elemMatch inside an aggregate because its a query.
How do I achieve this? I've seen people saying to use a $filter but I have no clue how to make that.

$elemMatch is not required here. This can be achieved only using $match as shown below.
const mongoose = require("mongoose");
Ticket.aggregate([{
"$lookup": {
"from": User.collection.name,
"localField": "creator",
"foreignField": "_id",
"as": "creator"
}
},
{
"$unwind": "$creator"
},
// Modified code
{
"$match": {
"creator.organizations": mongoose.Types.ObjectId("60a77d5b57d8c960829a0343")
}
},
{
"$set": {
"creator": "$creator._id"
}
},
])

Related

how can I query multiple table to get the data from collections in mongodb using pymongo

I have 5 collections
user : { "id": 1}
group: { "gid": 13, "name": "group1"}
subgroup: {"sgid": 22, "gid": 13, "name": "sgroup1"}
maps : {"userId": 1, "id": 10, "mapId": 13}
data: {"sgid": 22, "somedata": "somedata"}
I have the current user id, now using userId I need to get mapId from maps, using mapId get the sgid from subgroup, from sgid get the data, along with the data i need groupname and subgroup name.
I think you need something like this:
The trick basically is to use the $lookup to do the "join" and additionally $unwind to deconstruct the result (also $lookup also works with an array).
And the last stage is $project to get only values you want.
db.user.aggregate([
{
"$lookup": {
"from": "maps",
"localField": "id",
"foreignField": "userId",
"as": "map"
}
},
{
"$unwind": "$map"
},
{
"$lookup": {
"from": "subgroup",
"localField": "map.mapId",
"foreignField": "gid",
"as": "subgroup"
}
},
{
"$unwind": "$subgroup"
},
{
"$lookup": {
"from": "data",
"localField": "subgroup.sgid",
"foreignField": "sgid",
"as": "data"
}
},
{
"$unwind": "$data"
},
{
"$lookup": {
"from": "group",
"localField": "subgroup.gid",
"foreignField": "gid",
"as": "group"
}
},
{
"$unwind": "$group"
},
{
"$project": {
"data": "$data.somedata",
"groupname": "$group.name",
"subgroupname": "$subgroup.name"
}
}
])
Example here
By the way, if you need 4 ยด$lookup` maybe Mongo is not the best DB (but this is only an opinion).

MongoDB Aggregation use value from Match Object in pipelin

i'm using the following aggregation:
const aggregate = [
{
$match: {
mainCatId: new ObjectId(catId),
},
},
{
"$lookup": {
"from": "products",
"pipeline": [
{ "$match": { "subCategory": '$_id' } },
],
"as": "products"
}
},
{ "$unwind": "$products" }
];
The problem is that i have to match the id of each doc in the pipeline section but this is not working. So the question is how can i match the id i"m getting from the match above
Is the syntax below, which uses the traditional syntax for single join conditions, what you are looking for?
const aggregate = [
{
$match: {
mainCatId: new ObjectId(catId),
},
},
{
"$lookup": {
"from": "products",
localField: "subCategory",
foreignField: "_id",
"as": "products"
}
},
{
"$unwind": "$products"
}
]
If not, please provide sample documents and further details about the ways in which your current approach (and this proposed solution) are not working.

mongoDB group documents after unwind

In my mongodb books collection I have documents that look like:
{
_id: ObjectId(625efa44f1ba751c8275ea51),
contributors:[ObjectId(625efa44f1ba751c8275ea52), ObjectId(625efa44f1ba751c8275ea53)]
//other fields
}
And I want to do a query that returns me documents like:
{
_id: ObjectId(625efa44f1ba751c8275ea51),
contributors:[
{
_id: ObjectId(625efa44f1ba751c8275ea52),
first_name: 'Luigi'
//many other fields
},
{
_id: ObjectId(625efa44f1ba751c8275ea53),
first_name: 'Mario'
//many other fields
},
]
//other fields
}
I did an unwind on contributors and a lookup with my users collection and now I need to group them. I haven't used it before but I did something like:
{
$group:{
_id: '_id'
}
}
But I don't know what to do next in order to preserve all the fields from books and also from users.
Do you have any idea?
If the $lookup result is small enough (<16MB document size limit),
you can simply do a $lookup.
db.books.aggregate([
{
"$lookup": {
"from": "users",
"localField": "contributors",
"foreignField": "_id",
"as": "contributors"
}
}
])
Here is the Mongo Playground for your reference.
If the $lookup result will exceed 16 MB limit, you can still $unwind. Just use $firstto regroup the other fields after the$unwind`
db.books.aggregate([
{
"$lookup": {
"from": "users",
"localField": "contributors",
"foreignField": "_id",
"as": "contributors"
}
},
{
"$unwind": "$contributors"
},
{
"$group": {
"_id": "_id",
bookName: {
$first: "$bookName"
},
"contributors": {
"$push": "$contributors"
}
}
}
])
Here is the Mongo playground for your reference.

Mongodb 3.4 version can not Join on string to an ObjectId field [duplicate]

I have two collections
User
{
"_id" : ObjectId("584aac38686860d502929b8b"),
"name" : "John"
}
Role
{
"_id" : ObjectId("584aaca6686860d502929b8d"),
"role" : "Admin",
"userId" : "584aac38686860d502929b8b"
}
I want to join these collection based on the userId (in role collection) - _id ( in user collection).
I tried the below query:
db.role.aggregate({
"$lookup": {
"from": "user",
"localField": "userId",
"foreignField": "_id",
"as": "output"
}
})
This gives me expected results as long as i store userId as a ObjectId. When my userId is a string there are no results.
Ps: I tried
foreignField: '_id'.valueOf()
and
foreignField: '_id'.toString()
. But no luck to match/join based on a ObjectId-string fields.
Any help will be appreciated.
You can use $toObjectId aggregation from mongodb 4.0 which converts String id to ObjectId
db.role.aggregate([
{ "$lookup": {
"from": "user",
"let": { "userId": "$_id" },
"pipeline": [
{ "$addFields": { "userId": { "$toObjectId": "$userId" }}},
{ "$match": { "$expr": { "$eq": [ "$userId", "$$userId" ] } } }
],
"as": "output"
}}
])
Or you can use $toString aggregation from mongodb 4.0 which converts ObjectId to String
db.role.aggregate([
{ "$addFields": { "userId": { "$toString": "$_id" }}},
{ "$lookup": {
"from": "user",
"localField": "userId",
"foreignField": "userId",
"as": "output"
}}
])
This is not possible as of MongoDB 3.4. This feature has already been requested, but hasn't been implemented yet. Here are the corresponding tickets:
SERVER-22781: Allow $lookup between ObjectId (_id.str) and
string
SERVER-24947: Need a type conversion mechanism for booleans,
ISODates, ObjectID
For now you'll have to store userId as ObjectId
EDIT
The previous tickets were fixed in MongoDB 4.0. You can now achieve this with the folowing query:
db.user.aggregate([
{
"$project": {
"_id": {
"$toString": "$_id"
}
}
},
{
"$lookup": {
"from": "role",
"localField": "_id",
"foreignField": "userId",
"as": "role"
}
}
])
result:
[
{
"_id": "584aac38686860d502929b8b",
"role": [
{
"_id": ObjectId("584aaca6686860d502929b8d"),
"role": "Admin",
"userId": "584aac38686860d502929b8b"
}
]
}
]
try it online: mongoplayground.net/p/JoLPVIb1OLS
I think the previous answer has an error on the $toObjectId case. The let statement applies to the db collection on which the function aggregate is called (i.e 'role') and not on the collection pointed by "from" (i.e 'user').
db.role.aggregate([
{ "$lookup": {
"let": { "userObjId": { "$toObjectId": "$userId" } },
"from": "user",
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$_id", "$$userObjId" ] } } }
],
"as": "userDetails"
}}
])
Or
db.role.aggregate([
{ "$project": { "userObjId": { "$toObjectId": "$userId" } } },
{ "$lookup": {
"localField": "userObjId",
"from": "user",
"foreignField": "$_id",
"as": "userDetails"
}}
])
And
db.user.aggregate([
{ "$project": { "userStrId": { "$toString": "$_id" }}},
{ "$lookup": {
"localField": "userStrId",
"from": "role",
"foreignField": "userId",
"as": "roleDetails"
}}
])
For example we have two collections:
Authors like {'_id': ObjectId(), name: 'John Smith'}
Messages like {'_id': ObjectId(), text: 'Message Text', authorId: 'stringId'}
And we need to get messages with author names and other data
Then:
db.messages.aggregate([
{
$addFields: {
'$authorObjectId': {$toObjectId: $authorId}
}
},
{
$lookup: {
from: 'authors',
localField: '$authorObjectId',
foreignField: '_id',
as: 'author'
}
}
])
Explained:
Our aggregation pipeline has two steps:
First: We add additional field to messages which contains converted string authorId to ObjectId()
Second: We use this field as localField (in messages) to be compared with foreignField '_id' (in authors)

MongoDB: Lookup - Collections with Same Fields

I've two collections Users and Notes. Both collections contain an id property and the Notes collection has an userid that is the id of some user on the Users collection.
Now, I'm trying to aggregate (join) some user information into Notes:
db.getCollection("Notes").aggregate(
{
"$lookup": {
"from": "Users",
"let": {
"idForeignField": "$id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$and": [{
"$eq": ["$userid", "$$idForeignField"]
}]
}
}
}
],
"as": "Users#joined"
}
}
);
What I get in a empty Users#joined array. Why? Shouldn't my query work? Is the problem caused by the fact that both collections have an id property? If yes how can I tell let and match what is the right collection?
Update: alternatively a simpler query works just fine:
db.getCollection("Notes").aggregate(
{
$lookup:
{
from: "Users",
localField: "userid",
foreignField: "id",
as: "Users#joined"
}
}
);
However I would like to do it with let and a pipeline in order to add more match conditions.
Thank you.
Your let variable must be userid
db.getCollection("Notes").aggregate([
{ "$lookup": {
"from": "Users",
"let": { "ifForeignField": "$userid" },
"pipeline": [
{ "$match": { "$expr": { "$and": [{ "$eq": ["$id", "$$ifForeignField"] }] }}}
],
"as": "Users#joined"
}}
])