How to use aggregrate in mongodb for array element - mongodb

I have two collection conversation and usercollection
MongoDB format of
conversation looks
[
{participants:["ram","shyam"], recentchat:"hello shyam"},
{participants:["ram","hari"], recentchat:"namaste"},
{participants:["jhon","raju"], recentchat:"what's up"}
]
Likewise usercollection looks
[
{"name" : "shyam", "address":"Kathmandu"},
{"name" : "hari", "address":"Dolakha"},
{"name:"jhon", "address":"Pokhara"},
]
The assumption is:
Ram is the user and it needs to access all the conversation list
I want to use aggregate method of mongodb for joining two collection and get the result in following format.
[
{friend:{name:"shyam",address:"Kathmandu"}, recentchat:"hello shyam"},
{friend:{name:"hari", address:"Dolakha"},recentchat:"namaste"}
]

If you are using MongoDB 3.2+ then you can use $lookup aggregator for that.
Example:
db.getCollection('conversation').aggregate([
{$match: {"participants":"ram"}},
{$lookup: {
from: "usercollection",
localField: "participants",
foreignField: "name",
as: "friend"
}},
{$project: {"friend":1, "recentchat": 1, "_id":0}}
]);
I use:
$match - for select the parent document.
$lookup - for data collecting from usercollection collection
$project - for definig which fields must be included into document

Get the recent chat for particular user from conversation collection
var recentChat = db.conversation.find({participants:"shyam"}).sort({$natural:-1}).recentchat
Update recent chat for that user in usercollection
db.usercollection.update({name:"shyam"}, {$set:{ recentchat: recentChat }})

Related

In mongodb, how to add field from one collection document to another collection document based on criteria

I am having 2 collections -
Collection name is content
Collection name is surveys
I actually want to add the "type" information from the content collection to every content in every survey in the "surveys" collection.
I know in SQL we have to join these 2 tables first on _id and content_id commun columns and then add type column to suryeys table but not sure how should i do it here. I want the type value from content collection to add in every content field inside surveys collection. Please help me.
One option is:
Using $lookup to get the data from content collection
Merge the arrays using $mergeObjects and $indexOfArray. After the lookup, the content data is on someField array. This step finds the releant content from someField and put it inside the matching survey's content item, under the key type. This step also removes someField.
Format the result using $map. This step uses $map to iterate over the content array and format the data under type to contain only the wanted part.
Save it back using $merge. This step saves the result back into the collection.
db.surey.aggregate([
{$lookup: {
from: "content",
localField: "content.content_id",
foreignField: "_id",
as: "someField"
}},
{$set: {
someField: "$$REMOVE",
content: {$map: {
input: "$content",
in: {$mergeObjects: [
"$$this",
{type: {
$arrayElemAt: [
"$someField",
{$indexOfArray: ["$someField._id", "$$this.content_id"]}
]
}}
]}
}}
}},
{$set: {content: {$map: {
input: "$content",
in: {$mergeObjects: ["$$this", {type: "$$this.type.type"}]}
}}}},
{$merge: {into: "surey"}}
])
See how it works on the playground example
In mongoose (I assume you could be using mongoose, sind you added the tag) you can specify the relation in Schema definition and just just populate to add these fields to the output.
If you are not using mongoose you can use $lookup (https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/) in your aggregation pipeline.
{
from: "content",
localField: "content._id",
foreignField: "_id",
as: "someField"
}

Query a reference to an object stored in a document mongoDB

I am wondering how exactly I would build query to filter based on the attributes of a stored document within a document using MongoDB.
Example:
My first collection is called Movies, it has a document as so:
_id: ObjectID("5d872b7f927f2538e4eefbf5")
mvNumb : "1"
Director : ObjectID("5d8abd243372eb2850ad71e7")
The second collection is called Director, and the director has the following fields:
_id: ObjectID("5d8abd243372eb2850ad71e7")
Name : "Sam"
What I am trying to do, is filter al my movies based on their director name. An ideal query would look like :
db.Movies.Find({"Director.Name : "Sam"})
Try;
db.directors.aggregate([
{
$match: {
Name: "Sam"
}
},
{
$lookup: {
from: "movies",
localField: "_id",
foreignField: "Director",
as: "movies"
}
},
{
$project: {
_id: 0,
movies: 1
}
}
])
You can first get the target director with Name using $match. Then find the movies that director is affiliated with using $lookup, which is essentially a join-like operation, that fetches all such movies, and puts them under a movies field. Then just project those fields only.
Check on mongoplayground

$lookup : computed foreinField workaround?

For an existing mongo database, the link between 2 collections is done by :
collA : field collB_id
collB : field _id = ObjectId("a string value")
where collB_id is _id.valueOf()
i.e. : the value of collB_id of collA is "a string value"
but in a $lookup :
localField: "collB_id",
foreignField: _id.valueOf(),
don't work, so what can I do ?
Mongodb v3.6
If i understood you correctly, you have two collections where documents from first collection (collA) reference documents from second collection (collB). And the problem is that you store reference as a string value of that objectId, so you cant use $lookup to join those docs.
collA:
{
"_id" : ObjectId(...),
"collB_id" : "123456...",
...
}
collB:
{
"_id" : ObjectId("123456..."),
...
}
If you are using mongo 4.0+ you can do it with following aggregation:
db.getCollection('collA').aggregate([
{
$addFields: {
converted_collB_id: { $toObjectId: "$collB_id" }
}
},
{
$lookup: {
from: 'collB',
localField: 'converted_collB_id',
foreignField: '_id',
as: 'joined'
}
}
]);
Mongo 4.0 introduces new aggregation pipeline operators $toObjectId and $toString.
That allows you to add new field which will be an ObjectId created from the string value stored in collB_id, and use that new field as localField in lookup.
I would strongly advise you not to store ObjectIds as strings.
You already experienced the problem with $lookup.
Also, size of ObjectId is 12 bytes while its hex representation is 24 bytes (that is twice the size). You will probably want to index that field as well, so you want it to be as small as possible.
ObjectId also contains timestamp which you can get by calling getTimestamp()
Make your life easier by using native types when possible!
Hope this helps!

MongoDB query, filter using cursor

I have two collections, one that has _id and UserId, and another that has UserId (same unique identifier) and "other data".
I want to filter the latter collection based on a list of _ids from the former collection.
Can someone provide an example query for this scenario?
The only way to 'join' collections in MongoDB is a $lookup aggregation stage (available in version 3.2).
firstCollection.aggregate([
{ $match: { _id: {$in: [1,2,3] }}}, // filter by _ids
{
$lookup:
{
from: "secondCollection",
localField: "UserId",
foreignField: "UserId",
as: "data"
}
}
])
That will add 'data' field to the documents from the first collection which will contain all related documents from second collection. If relation is not 1:1, you can add $unwind stage to flatten results:
{$unwind: "$data"}

List of Id's with no dependency in Mongo Collection

I have a scenario in spring-mongo query. Mongo version is 3.2
Application have two collections (Collection A and Collection B).
**Sample contents**
Collection A :: {"_id":1, "name":"content 1"}...{"_id":100, "name":"content 100"}
Collection B :: {"_id":1, "name":"parent 1", "a":[1,2,58,67]}
{"_id":2, "name":"parent 2", "a":[2,85,96,99]}
Collection B holds reference ids of Collection A as a array.
Scenario:
I will past list of ids of Collection A to the query: I need to get list of ids of Collection A which are not associated anywhere in Collection B.
how to achieve this?
I am planning to proceed with Aggregation with following query.. Looking up with
preserveNullAndEmptyArrays saving my day.
db.a.aggregate([
{
$match: { "_id":{$in:["1","5","10"]} }
},
{
$lookup:
{
from: "b",
localField: "_id",
foreignField: "a",
as: "moneyid"
}
},
{
$unwind:
{"path":"$moneyid", "preserveNullAndEmptyArrays":true}
},
{
$match:{
"moneyid":{$eq:null}
}
}
])
It can be done using the following approach
1) Find Unique Id's in Collection B which are in array "a":[]
2) Execute a find query in Collection A - Use $nin in the find query and pass all the ids which you obtained from array "a":[] from Collection B
Note:If you need to find matching documents between two collections then you can use $lookup which works like a left outer join.