Adding or, and in mongodb aggregate $lookup - database

How can i do something like this:
$or: [
{ $and: [{ author: new mongoose.Types.ObjectId(author) }, { friend_id: new mongoose.Types.ObjectId(friend_id) }] },
{ $and: [{ author: new mongoose.Types.ObjectId(friend_id) }, { friend_id: new mongoose.Types.ObjectId(author) }] },
],
In a aggregate $lookup function?
Can you help me? Thanks!
Heres my code
let rqs:any = this.friendrequestModel.aggregate([
{
$lookup: {
from: "users",
localField: "author",
foreignField: "_id",
as: "userdata",
},
},
]);

Related

Map array result in mongodb aggregation

I have the following MongoDB query:
const vaccination = await Schedule.aggregate([
{ $match: { status: ScheduleStatus.Published } },
{ "$unwind": { "path": "$vaccines", "preserveNullAndEmptyArrays": true } },
{
"$group": {
"_id": "$vaccines.vaccine",
"count": { "$sum": "$vaccines.stok" },
}
},
{
$lookup: { from: 'vaccines', localField: '_id', foreignField: '_id', as: 'vaccine' },
},
{
$project: {
"count": 1,
"vaccine": { "$arrayElemAt": ["$vaccine.name", 0] }
}
}
]);
and return the following results :
[
{
"_id": "61efd8a812432135c08a748d",
"count": 20,
"vaccine": "Sinovac"
}
]
is there a way I can make the output to be an array of values like:
[["Sinovac",20]]
Thanks sorry for my bad english
So you can't get the exact structure you asked for, by definition the aggregation framework returns an array of "documents", a document is in the form of {key: value}, What you can do however is return the following structure:
[
{
"values": [
"61efd8a812432135c08a748d",
20,
"Sinovac"
]
}
]
With this pipeline:
db.collection.aggregate([
{
$project: {
_id: 0,
values: {
$map: {
input: {
"$objectToArray": "$$ROOT"
},
as: "item",
in: "$$item.v"
}
}
}
}
])
Mongo Playground

Update all objects in nested array with values from other collection

I have a collection of vehicles with the following car structure:
{
"_id": {}
brand : ""
model : ""
year : ""
suppliers : [
"name": "",
"contact": ""
"supplierId":"",
"orders":[], <-- Specific to the vehicles collection
"info":"" <-- Specific to the vehicles collection
]
}
And a Suppliers collection with a structure like:
{
"name":"",
"contact":"",
"_id":{}
"internalId":"",
"address":"",
...
}
I need to add a new field in the suppliers array within each document in the vehicles collection with the internalId field from the supplier in the suppliers collection that has the same _id.
if the supplier array has a document with the id 123, i should go to the suppliers collection and look for the supplier with the id 123 and retrieve the internalId. afterwards should create the field in the supplier array with that value.
So that i end up with the vehicles collection as:
{
"_id": {}
brand : ""
model : ""
year : ""
suppliers : [
"name": "",
"contact": ""
"supplierId":""
"internalId":"" <-- the new field
]
}
Tried:
db.vehicles.aggregate([
{
"$unwind": { "path": "$suppliers", "preserveNullAndEmptyArrays": false }
},
{
"$project": { "supplierObjId": { "$toObjectId": "$suppliers.supplierId" } }
},
{
"$lookup":
{
"from": "suppliers",
"localField": "supplierObjId",
"foreignField": "_id",
"as": "supplierInfo"
}
},{
"$set": {
"suppliers.internalId": "$supplierInfo.internalid"
}}
])
But it is adding the new field, to the returned values instead to the array item at the collection.
How can i achieve this?
But it is adding the new field, to the returned values instead to the array item at the collection.
The .aggregate method does not update documents, but it will just format the result documents,
You have to use 2 queries, first aggregate and second update,
I am not sure i guess you want to execute this query for one time, so i am suggesting a query you can execute in mongo shell,
Aggregation query:
$lookup with pipeline, pass suppliers.supplierId in let
$toString to convert object id to string type
$match the $in condition
$project to show required fields
$map to iterate loop of suppliers array
$reduce to iterate loop of suppliers_data array and find the matching record by supplierId
$mergeObjects to merge current object properties with new property internalId
Loop the result from aggregate query using forEach
Update Query to update suppliers array
db.vehicles.aggregate([
{
$lookup: {
from: "suppliers",
let: { supplierId: "$suppliers.supplierId" },
pipeline: [
{
$match: {
$expr: {
$in: [{ $toString: "$_id" }, "$$supplierId"]
}
}
},
{
$project: {
_id: 0,
supplierId: { $toString: "$_id" },
internalId: 1
}
}
],
as: "suppliers_data"
}
},
{
$project: {
suppliers: {
$map: {
input: "$suppliers",
as: "s",
in: {
$mergeObjects: [
"$$s",
{
internalId: {
$reduce: {
input: "$suppliers_data",
initialValue: "",
in: {
$cond: [
{ $eq: ["$$this.supplierId", "$$s.supplierId"] },
"$$this.internalId",
"$$value"
]
}
}
}
}
]
}
}
}
}
}
])
.forEach(function(doc) {
db.vehicles.updateOne({ _id: doc._id }, { $set: { suppliers: doc.suppliers } });
});
Playground for aggregation query, and Playground for update query.
It looks like one way to solve this is by using $addFields and $lookup. We first flatten any matching suppliers, then add the property, then regroup.
You can find a live demo here via Mongo Playground.
Database
Consider the following database structure:
[{
// Collection
"vehicles": [
{
"_id": "1",
brand: "ford",
model: "explorer",
year: "1999",
suppliers: [
{
name: "supplier1",
contact: "john doe",
supplierId: "001"
},
{
name: "supplier2",
contact: "jane doez",
supplierId: "002"
}
]
},
{
"_id": "2",
brand: "honda",
model: "accord",
year: "2002",
suppliers: [
{
name: "supplier1",
contact: "john doe",
supplierId: "001"
},
]
}
],
// Collection
"suppliers": [
{
"name": "supplier1",
"contact": "john doe",
"_id": "001",
"internalId": "999-001",
"address": "111 main street"
},
{
"name": "supplier2",
"contact": "jane doez",
"_id": "002",
"internalId": "999-002",
"address": "222 north street"
},
{
"name": "ignored_supplier",
"contact": "doesnt matter",
"_id": "xxxxxxx",
"internalId": "xxxxxxx",
"address": "0987 midtown"
}
]
}]
Query
This is the query that I was able to get working. I'm not sure how efficient it is, or if it can be improved, but this seemed to do the trick:
db.vehicles.aggregate([
{
$unwind: "$suppliers"
},
{
$lookup: {
from: "suppliers",
localField: "suppliers.supplierId",
foreignField: "_id", // <---- OR MATCH WHATEVER FIELD YOU WANT
as: "vehicle_suppliers"
}
},
{
$unwind: "$vehicle_suppliers"
},
{
$addFields: {
"suppliers.internalId": "$vehicle_suppliers.internalId"
}
},
{
$group: {
_id: "$_id",
brand: {
$first: "$brand"
},
model: {
$first: "$model"
},
year: {
$first: "$year"
},
suppliers: {
$push: "$suppliers"
}
}
}
])
Results
Which returns:
[
{
"_id": "2",
"brand": "honda",
"model": "accord",
"suppliers": [
{
"contact": "john doe",
"internalId": "999-001",
"name": "supplier1",
"supplierId": "001"
}
],
"year": "2002"
},
{
"_id": "1",
"brand": "ford",
"model": "explorer",
"suppliers": [
{
"contact": "john doe",
"internalId": "999-001",
"name": "supplier1",
"supplierId": "001"
},
{
"contact": "jane doez",
"internalId": "999-002",
"name": "supplier2",
"supplierId": "002"
}
],
"year": "1999"
}
]

mongoose aggregate how to map multiple collections into one Array

I have four different collections. From which three are connected to one:
Collection_A = {
_id: 1
name: A
includes: [
{
_id: 1,
includes_id: 222,
},
{
_id: 2,
includes_id: 333
}
]
}
Collection_B = {
_id: 222,
type: Computer,
name: Computer,
ref_id: 1
}
Collection_C = {
_id: 333,
type: Human,
name: Human,
ref_id: 1
}
Collection_D = {
_id: 444,
type: Animal,
name: Animal,
ref_id: 1
}
So collection A can include Collection B, C and D in the includes object. It includes minimum one of the collections.
So in the includes object in Collection A is the includes_id, which is the _id in Collection B, C and D.
The _id in Collection A is the ref_id in Collection B, C and D.
What my problem right now is, that aggregate takes only the last mapped collection.
My code right now is following:
Collection_A.aggregate([
{
$lookup: {
from: "collectionb",
localField: "includes.includes_id",
foreignField: "_id",
as: "colb",
},
},
{
$lookup: {
from: "collectionc",
localField: "includes.includes_id",
foreignField: "_id",
as: "colc",
},
},
{
$project: {
_id: 1,
status: 1,
type: 1,
includes_list: {
$map: {
input: "$includes",
as: "i",
in: {
$arrayElemAt: [
{
$filter: {
input: "$colb",
cond: {
$eq: ["$$this._id", "$$i.includes_id"],
},
},
},
0,
],
$arrayElemAt: [
{
$filter: {
input: "$colc",
cond: {
$eq: ["$$this._id", "$$i.includes_id"],
},
},
},
0,
],
},
},
},
},
},
]);
I tried to make the $lookup as the same on every lookup, but so it only took the last looked up data, and the others where shown as null.
So I made $lookup as unique, and put two ins in map, but then also the last looked up data was shown, and the others where null.
When I do the mapping like that:
includes_list: {
$map: {
input: "$icludes",
as: "i",
in: {
{
Col_A : {
$arrayElemAt: [
{
$filter: {
input: "$A",
cond: {
$eq: ["$$this._id", "$$i.includes"],
},
},
},
0,
],
},
Col_B : {
$arrayElemAt: [
{
$filter: {
input: "$B",
cond: {
$eq: ["$$this._id", "$$i.includes"],
},
},
},
0,
],
}
}
},
},
}
It workes. But not with the right output, because I need includes_list within one array.
My desired output is like following:
{
includes: [
{
_id: 1,
name: Computer,
includes_list: [
{
_id: 222,
type: Computer,
name: Computer,
ref_id: 1
},
{
_id: 333,
type: Human,
name: Human,
ref_id: 1
},
]
},
{
_id: 2,
name: Animal,
includes_list: [
{
_id: 333,
type: Human,
name: Human,
ref_id: 2
},
]
}
]
}
Would appreciate any help!
For this kind of situation,
$facet help to categorize the incoming data
db.Collection_A.aggregate([
{ $unwind: "$includes },
{
"$facet": {
"joinB": [
{
"$lookup": {
"from": "Collection_B", "localField": "includes.includes_id",
"foreignField": "_id", "as": "includes.includes_list"
}
},
{
"$group": {
"_id": "$_id",
"name": { "$first": "$name" },
includes: { $push: "$includes" }
}
}
],
"joinC": [
{
"$lookup": {
"from": "Collection_C", "localField": "includes.includes_id",
"foreignField": "_id", "as": "includes.includes_list"
}
},
{
"$group": {
"_id": "$_id",
"name": { "$first": "$name" },
includes: { $push: "$includes" }
}
}
],
"joinD": [
{
"$lookup": {
"from": "Collection_D", "localField": "includes.includes_id",
"foreignField": "_id", "as": "includes.includes_list"
}
},
{
"$group": {
"_id": "$_id",
"name": { "$first": "$name" },
includes: { $push: "$includes" }
}
}
],
}
},
{
$project: {
combined: {
"$concatArrays": [ "$joinB", "$joinC", "$joinD" ]
}
}
},
{ "$unwind": "$combined" },
{
"$replaceRoot": { "newRoot": "$combined" }
},
{
"$project": {
_id: 1,
name: 1,
includes: {
$filter: {
input: "$includes",
cond: {
$ne: [ "$$this.includes_list",[] ]
}
}
}
}
}
])
Working Mongo playground
Note: I feel this is kind of Anti pattern you follow. If you are in early stage of the project, better change the structure if I'm not mistaken.

How to do lookup with an array in mongoDB

today I started working with MongoDB. I have created two collections: Restaurant and OpeningHours. I have inserted data to the database with the following code:
db.OpeningHours.insert({
day: "Sunday",
from: "10.00am",
to: "16.00pm"
});
db.Restaurant.insert({
name: "Restaurant01",
openingHoursId:
[
{id: db.OpeningHours.find({day: "Sunday", from: "10.00am", to: "16.00pm"})[0]._id},
]
});
The restaurant contains an array of OpeningHours ids. I want to write a query with lookup so I get all the data from the Restaurant and the data for corresponding opening hours. Here is my code so far and if I run it I get an error: Command failed...
db.Restaurant.aggregate([
{
$unwind: "$openingHoursId",
$lookup:
{
from: "OpeningHours",
localField: "id",
foreignField: "_id",
as: "RestaurantHours"
}
}
])
The expected result I want is something like this:
{
"_id": ObjectId("5c43b6c8d0fa3ff24621f749"),
"name": "Restaurant01",
"openingHoursId":
[
{
"id": ObjectId("5c43b6c8d0fa3ff2462fg93e")
}
],
"RestaurantHours" :
[
{
"_id": ObjectId("5c43b6c8d0fa3ff2462fg93e"),
"day": "Sunday",
"from": "10.00am",
"to": "16.00pm"
}
]
}
Your localField should be openingHoursId.id not only id
db.Restaurant.aggregate([
{ "$unwind": "$openingHoursId" },
{ "$lookup": {
"from": "OpeningHours",
"localField": "openingHoursId.id",
"foreignField": "_id",
"as": "openingHoursId.RestaurantHours"
}},
{ "$unwind": "$openingHoursId.RestaurantHours" },
{ "$group": {
"_id": "$_id",
"name": { "$first": "name" },
"openingHoursId": { "$push": "openingHoursId" }
}}
])

MongoDB how to $project (limit fields) from the $lookup remote collection?

I want to $lookup for a remote collection like SQL Join but with Mongo. And I don't want all of the keys from the remote document to be pulled to the origin collection - just some specific keys.
This is what I have tried:
[
{
$lookup: {
from: "tables",
localField: "type",
foreignField: "_id",
as: "type"
}
},
{
$unwind: "$type"
},
},
{
$project: {
"type.title": 1
}
}
]
However this prints only "type.title" and ignores all of other keys even from the origin document.
Is there any way to tell MongoDB to pull only specific fields from the remote collection?
You can use below aggregation with mongodb 3.6 and above
[
{ "$lookup": {
"from": "tables",
"let": { "type": "$type" },
"pipeline": [
{ "$addFields": { "owners": { "$cond": { "if": { "$ne": [ { "$type": "$owners" }, "array" ] }, "then": [], "else": "$owners" } } }},
{ "$match": { "$expr": { "$eq": ["$_id", "$$type"] }}},
{ "$project": { "title": 1 }}
],
"as": "type"
}},
{ "$unwind": "$type" }
]

Resources