Is it possible to compare string with ObjectId via $lookup - mongodb

table1 has a field string "value" and table2 has a field "value" as ObjectId, Is it possible to do a query like this or how to write
table1.aggregate([
{
$lookup: {
from: "table2",
localField: "value",
foreignField: "_id",
as: "test"
}
}
])

As far I know to join collections using $lookup operator in MongoDB data type should be same. If type mismatch then $lookup will not work. So to join you should use those field that are same type because it check equality.
The $lookup stage does an equality match between a field from the
input documents with a field from the documents of the “joined”
collection
If localField type object then foreignField should be object
If localField type string then foreignField should be string
If localField type number then foreignField should be number
$lookup Documentation

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"
}

Get matched and unmatched count from another collection mongodb

I have two collection present in mongodb database. one status column is common. I need matched and unmatched count from both the collection based on status column.
I have written some code but it fetching only matched count.
db.properties.aggregate([
{
$lookup: {
from: "old_properties_data",
localField: "identifier",
foreignField: "identifier",
as: "col2docs"
}
},
{"$group" : {_id:"_id", count:{$sum:1}}}
])
You can first $group by identifier to get the total match count. Then You can use uncorreleated subquery in $lookup to get the total size of old_properties_data. Finally do a $subtract to get the total unmatched count.
Here is the Mongo Playground for your reference.

robo 3t - Aggregate query on array fails

Setup:
Documents in parent collection have a field children with child identifiers. Documents in the child collection have the identifier in a customId field not _id. A single identifier from the 'children' field may match multiple children.
What's wrong:
This query
db.getCollection('parent').aggregate({$lookup: {from: "child", localField: "children", foreignField:"customId", as:"joinedChildren"}})
runs correctly in mongo shell but causes
Error:
TypeError: pipeline[(pipeline.length - 1)] is undefined :
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1292:9
DBCollection.prototype.aggregate#:1:355
#(shell):1:1
in robo3T.
The problem went away when I wrap the query in []
db.getCollection('parent').aggregate([{$lookup: {from: "child", localField: "children", foreignField:"customId", as:"joinedChildren"}}])
db.Parent.Aggregate([{$lookup :
{
From :"child",
localFied:"children",
foreignField:"customId",
as : "joinedchildren"
}
}])

$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"}