Related
`I have two collections in MongoDB. One is users and the other is companies. I need to be able to populate the users.endpoints with the endpoints from companies that match ObjectID's which are in the user.endPoints property. It is a bit hard for me to wrap my head around considering there are two steps.
This is the companies collection.
{
"_id": {
"$oid": "63ed39162bfc2cf8065b76cf"
{
"_id": {
"$oid": "63ed39162bfc2cf8065b76cf"
},
"companyName": "Sowegatel",
"amount": 0,
"signalwireSid": "id",
"numbers": [
{
"number": "+12068133580",
"_id": {
"$oid": "63ed39c72bfc2cf8065b76f0"
},
"createdAt": {
"$date": {
"$numberLong": "1676491207931"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1676491207931"
}
}
}
],
"endPoints": [
{
"userName": "4009",
"_id": "fb1d0ef9-c713-400e-a3c9-cbc6823aaf57"
},
{
"userName": "4019",
"_id": "506e710d-14a6-4345-bc89-8488af4cabe4"
},
{
"userName": "4020",
"_id": "c80bd1ab-ca8d-4649-9d35-56d64ef8fab5",
"type": "sip_endpoint"
}
],
"__v": 6
} },
"companyName": "Sowegatel",
"amount": 0,
"signalwireSid": "id",
"numbers": [
{
"number": "+12068133580",
"_id": {
"$oid": "63ed39c72bfc2cf8065b76f0"
},
"createdAt": {
"$date": {
"$numberLong": "1676491207931"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1676491207931"
}
}
}
],
"endPoints": [
{
"userName": "4009",
"_id": "fb1d0ef9-c713-400e-a3c9-cbc6823aaf57"
},
{
"userName": "4019",
"_id": "506e710d-14a6-4345-bc89-8488af4cabe4"
},
{
"userName": "4020",
"_id": "c80bd1ab-ca8d-4649-9d35-56d64ef8fab5",
"type": "sip_endpoint"
}
],
"__v": 6
}
This is the users collection.
{
"_id": {
"$oid": "63ed39162bfc2cf8065b76d2"
},
"firstName": "mark",
"lastName": "thomas",
"email": "mt#st.com",
"password": "",
"companyId": {
"$oid": "63ed39162bfc2cf8065b76cf"
},
"active": true,
"role": "companyAdmin",
"tokens": [],
"__v": 2,
"endpoints": [
// I need to populate to endpoints here!!!! //
"c80bd1ab-ca8d-4649-9d35-56d64ef8fab5"
]
}
I can do a single step aggregation, but this is a little complicated for me.`
Got thrown into a Typescript/Mongodb project and am having some trouble getting started. I've been asked to convert data from
{
"_id": {
"$oid": "636c0a8dce0eb36dc13e63b3"
},
"status": "active",
"companies": [
{
"$oid": "5f64bd6785fad3e83771bb4f"
}
],
"whitelists": [],
"taxonomies": [
{
"$oid": "6080538565c2036dbeadcdfa"
},
{
"$oid": "6080538765c2036dbeadce07"
},
{
"$oid": "6080538d65c2036dbeadce44"
},
{
"$oid": "6080538d65c2036dbeadce46"
}
],
"itemType": "unit",
"attributes": [
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db4"
},
"type": "ingredients:statement",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "ingredients:statement#0",
"value": {
"footnotes": [],
"statement": "ingredients: water, butter, lemon juice, wheat flour, egg yolks, sea salt, xanthan gum, turmeric, carotenes."
}
},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db5"
},
"type": "allergens",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "allergens#0",
"value": {
"type": "contains",
"statement": "contains something"
}
},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db5"
},
"type": "allergens",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "allergens#0",
"value": {
"type": "something",
"statement": "may contain something"
}
},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db6"
},
"type": "variants:flavors:raw",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "variants:flavors:raw#0",
"value": [
"hollandaise sauce"
]
}
],
"dataUpdatedDate": {
"$date": {
"$numberLong": "1668025007811"
}
}
}
To something like this, merging objects with the same "type" "b"
{
"_id": {
"$oid": "636c0a8dce0eb36dc13e63b3"
},
"status": "active",
"companies": [
{
"$oid": "5f64bd6785fad3e83771bb4f"
}
],
"whitelists": [],
"taxonomies": [
{
"$oid": "6080538565c2036dbeadcdfa"
},
{
"$oid": "6080538765c2036dbeadce07"
},
{
"$oid": "6080538d65c2036dbeadce44"
},
{
"$oid": "6080538d65c2036dbeadce46"
}
],
"itemType": "unit",
"attributes": [
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db4"
},
"type": "ingredients:statement",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "ingredients:statement#0",
"value": {
"footnotes": [],
"statement": "ingredients: water, butter, lemon juice, wheat flour, egg yolks, sea salt, xanthan gum, turmeric, carotenes."
}
},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db5"
},
"type": "allergens",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "allergens#0",
"value": [{
"type": "contains",
"statement": "contains something"
},
{
"type": "something",
"statement": "may contain something"
}
]},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db6"
},
"type": "variants:flavors:raw",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "variants:flavors:raw#0",
"value": [
"hollandaise sauce"
]
}
],
"dataUpdatedDate": {
"$date": {
"$numberLong": "1668025007811"
}
}
}
My background is php/mysql. I understand the basics here just can't put it together. Any help would be greatly appreciated.
Thanks
I have the basics of the typescript started to grab all the appropriate documents. I don't know how to go about combining some of the data and updating it back to mongo
Here is the basic code I am starting with
makeScript({
module,
db: true,
init: async _argv => {
await Pv2.find({
attributes: {
$elemMatch: {
type: 'allergens'
},
},
})
.limit(10)
.cursor()
.eachAsync(async (oldPv2s: Pv2Doc | Pv2Doc[]) => {
// not sure what goes here
});
}
});
Node js using mongoose as ORM. I have set of documents like below
I want to calculate the average time for createdAt in ISO String(format) field in duration of hours on
grouping the category field. I have tried many ways to use aggregate but couldn't get the result.
[{
"_id": {
"$oid": "62dfc2cf25735e8b1b475ff1"
},
"numLikes": 0,
"numViews": 0,
"numShares": 0,
"hasUserLiked": false,
"title": "sample-broadcast",
"description": "broadcast-dewscription",
"projectId": {
"$oid": "62d903a5dade1714382b27af"
},
"content": [
{
"_id": {
"$oid": "62dfc2cf25735ee18d475ff2"
},
"downloadLink": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/51daead4-1134-4921-a544-fea845f03d1c/uploadbroadcast/1658831544",
"label": "lance-anderson-QdAAasrZhdk-unsplash (1).jpg",
"contentType": "jpeg"
}
],
"delivery": "scheduleBroadcast",
"category": "GENERAL_UPDATES",
"groupId": "51daead4-1134-4921-a544-fea845f03d1c",
"author": {
"userId": "83314517-9326-430f-9c4e-8fedb050e6b0",
"profilePic": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/4f670832-dfd7-43ee-a6ad-e2f43f48df6a/uploadprofilepic/1658239806",
"name": "Biswajit Rout"
},
"projectLogo": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/4f670832-dfd7-43ee-a6ad-e2f43f48df6a/uploadcompanylogo%2A/1658239764",
"createdAt": {
"$date": {
"$numberLong": "1658831567577"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1658831567577"
}
},
"__v": 0
},{
"_id": {
"$oid": "62dfebae25735e015f476dfb"
},
"numLikes": 0,
"numViews": 0,
"numShares": 0,
"hasUserLiked": false,
"title": "testing-broadcast",
"description": "testing-description",
"projectId": {
"$oid": "62d903a5dade1714382b27af"
},
"content": [
{
"_id": {
"$oid": "62dfebae25735e291c476dfc"
},
"downloadLink": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/51daead4-1134-4921-a544-fea845f03d1c/projectimages0/1658842001",
"label": "lance-anderson-QdAAasrZhdk-unsplash (1).jpg",
"contentType": "jpeg"
},
{
"_id": {
"$oid": "62dfebae25735e0321476dfd"
},
"downloadLink": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/51daead4-1134-4921-a544-fea845f03d1c/projectimages1/1658842024",
"label": "Get_Started_With_Smallpdf.pdf",
"contentType": "pdf"
}
],
"delivery": "immediate",
"link": "http://localhost:3000",
"category": "GENERAL_UPDATES",
"groupId": "51daead4-1134-4921-a544-fea845f03d1c",
"author": {
"userId": "83314517-9326-430f-9c4e-8fedb050e6b0",
"profilePic": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/4f670832-dfd7-43ee-a6ad-e2f43f48df6a/uploadprofilepic/1658239806",
"name": "Biswajit Rout"
},
"projectLogo": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/4f670832-dfd7-43ee-a6ad-e2f43f48df6a/uploadcompanylogo%2A/1658239764",
"createdAt": {
"$date": {
"$numberLong": "1658842030827"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1658842030827"
}
},
"__v": 0
},{
"_id": {
"$oid": "62e144677fc76b0373f40152"
},
"numLikes": 0,
"numViews": 0,
"numShares": 0,
"hasUserLiked": false,
"title": "Broker Offer-1",
"description": "50% off on the membership for early birds. \n\nOffer Applied to first fifty users only",
"projectId": {
"$oid": "62d903a5dade1714382b27af"
},
"content": [
{
"_id": {
"$oid": "62e144677fc76b2b79f40153"
},
"downloadLink": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/51daead4-1134-4921-a544-fea845f03d1c/projectimages0/1658930275",
"label": "50-off-PNG-Picture.png",
"contentType": "png"
}
],
"delivery": "immediate",
"link": "",
"category": "OFFER_BROKERS",
"groupId": "51daead4-1134-4921-a544-fea845f03d1c",
"author": {
"userId": "83314517-9326-430f-9c4e-8fedb050e6b0",
"profilePic": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/4f670832-dfd7-43ee-a6ad-e2f43f48df6a/uploadprofilepic/1658239806",
"name": "Biswajit Rout"
},
"projectLogo": "https://builder-broadcast-media.s3.ap-south-1.amazonaws.com/builder-broadcast-media/4f670832-dfd7-43ee-a6ad-e2f43f48df6a/uploadcompanylogo%2A/1658239764",
"createdAt": {
"$date": {
"$numberLong": "1658930279871"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1658930279871"
}
},
"__v": 0
}]
My expected result should be like this
{
OFFER_BROKERS: 5 <hrs>,
GENERAL_UPDATES : 4 <hrs>
}
Do you want to convert milliseconds to real time and give it to orm
My documents look like this
{
"_id": {
"$oid": "62825f71005ce00c5f0235c1"
},
"user": "jon",
"roles": {
"User": 2001,
},
"STOCK ": [
{
"sku": "BLACK-M",
"productname": "BLACK",
"sendout": 0,
"recived": 1,
"totalinstock": 40,
"location": "B32",
"_id": {
"$oid": "62826016005ce00c5f0235c8"
}
},
{
"sku": "CJNS",
"productname": "89796363",
"sendout": 0,
"recived": 45,
"totalinstock": 0,
"location": "B232",
"_id": {
"$oid": "62836f2d56b4f1ac79c99b8d"
}
}
],
"ORDERS": [
{
"date": {
"$date": "2022-06-02T15:23:58Z"
},
"OrderNumber": "745607",
"City": "xxxxx",
"Address": "yyyyyy",
"Phone": "8678678678",
"Name": "Holly ",
"Trackingnumber": 40,
"ZipCode": 00000,
"Province": "New ",
"Quantity": [
1
],
"Product_Name": [
" pants pants"
],
"SKU": [
"CJNS"
],
"_id": {
"$oid": "6298d61ba6eeec72b78332a7"
}
},
{
"date": {
"$date": "2022-06-02T15:23:58Z"
},
"OrderNumber": "748840",
"City": "xxxxx",
"Address": "yyyyyy",
"Phone": "354353454",
"Name": "Michael",
"Trackingnumber": 0,
"ZipCode": 00000,
"Province": "North",
"Quantity": [
1
],
"Product_Name": [
" pants pants"
],
"SKU": [
"CJNS"
],
"_id": {
"$oid": "6298d61ba6eeec72b78332a9"
}
}
]
}
I successful to return all the objects in STOCK or all the objects in ORDERS
Through this query
const foundUser= await User.find({"user":req.body.user},("Orders") ).exec()
Now I want to filter the response to include only items where "Trackingnumber" is different from 0
For the sample data I expect to receive only
{
"date": {
"$date": "2022-06-02T15:23:58Z"
},
"OrderNumber": "748840",
"City": "xxxxx",
"Address": "yyyyyy",
"Phone": "354353454",
"Name": "Michael",
"Trackingnumber": 0,
"ZipCode": 00000,
"Province": "North",
"Quantity": [
1
],
"Product_Name": [
" pants pants"
],
"SKU": [
"CJNS"
],
"_id": {
"$oid": "6298d61ba6eeec72b78332a9"
}
}
You can use an aggregation pipeline with a $filter for this:
db.collection.aggregate([
{
$match: {
"user": "jon"
}
},
{
$project: {
ORDERS: {
$filter: {
input: "$ORDERS",
as: "item",
cond: {$ne: ["$$item.Trackingnumber", 0]}
}
}
}
}
])
Playground example
User.find({"Orders" : {"Trackingnumber": 0} })
I am having data from two different collections on which I am applying $lookup with $match which is working fine. What I am trying to do is on lookup's result I am applying $match again which is having some conditions. Here is the collection of User on which I am applying lookup.
Users collection :-
{
"_id": {
"$oid": "5b714631faaae220d7cc07cf"
},
"name": "abc",
"surname": "xyz",
"email": "abc#gmail.com",
"hotel_data": [
{
"location": {
"type": "Point",
"coordinates": [
30.7052881,
76.84470799999997
]
},
"_id": {
"$oid": "5b7fb8559849fd485dc47240"
},
"status": true,
"activityname": "Sparrow",
},
{
"location": {
"type": "Point",
"coordinates": [
30.733315,
76.779419
]
},
"_id": {
"$oid": "5b7f9ecb9960053dac7ce6f1"
},
"status": true,
"activityname": "Raj Hotel",
},
]
}
and this is my availabilities collection on which I am applying $lookup with $match.
{
"_id": {
"$oid": "5b867766d63c4e2cdd5534d2"
},
"businessid": {
"$oid": "5b7fb8559849fd485dc47240"
},
"userid": {
"$oid": "5b714631faaae220d7cc07cf"
},
"hotel_filters": [
{
"_id": {
"$oid": "5b867766d63c4e2cdd5534d3"
},
"hotelservice": [
{
"service_id": "5b472fff25556c3f02a875aa",
"service_name": "Pool",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d7"
}
},
{
"service_id": "5b472fdb25556c3f02a875a9",
"service_name": "AIR",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d8"
}
}
],
"location_type": [
{
"locationtype_id": "5b18f4d08c63f42019763b12",
"locationtype_name": "Scenic View",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d5"
}
},
{
"locationtype_id": "5b18f4e38c63f42019763b13",
"locationtype_name": "Central",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d6"
}
}
],
"hotel_type": [
{
"hoteltype_id": "5b081452edefe23318834a28",
"hoteltype_name": "3 Star",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d4"
}
}
]
}
]
}
and what I am trying to do is- after $lookup I only want those availabilities data which are having "hotelservice.service_id" and "location_type.locationtype_id" that I will pass. And I have tried this by applying this query to it(In this query I have only applied "hotelservice.service_id" but later on I also want to apply query with $and on "location_type.locationtype_id").
User.aggregate([
{
$match:{
"hotel_data.location": {
"$geoWithin": {
"$centerSphere": [
[30.7052881, 76.84470799999997], 50/ 6371
]
}
}
}
},{
"$unwind": "$hotel_data"
},
{
$match:{
"hotel_data.location": {
"$geoWithin": {
"$centerSphere": [
[30.7052881, 76.84470799999997], 50/ 6371
]
}
}
}
},
{
$lookup: {
from: "availabilities",
localField: "hotel_data._id",
foreignField: "businessid",
as: "availabilitiesdata"
}
},
{$match:{$and: [{"availabilitiesdata.hotel_filters.hotelservice.service_id":{$in:[5b472fb725556c3f02a875a8]}}]}}
], function(err, data) {
if (err) {
return res.send({data: err, status: false, msg:"No Hotel Found" });
}else{
return res.send({status: true, msg:"Hotel Found", data:data });
}
});
This query gives me result as follow.
{
"status": true,
"msg": "Hotel Found",
"data": [
{
"_id": "5b714631faaae220d7cc07cf",
"name": "abc",
"surname": "xyz",
"email": "abc#gmail.com",
"hotel_data": {
"location": {
"type": "Point",
"coordinates": [
30.7052881,
76.84470799999997
]
},
"_id": "5b7fb8559849fd485dc47240",
"status": true,
"activityname": "Sparrow",
},
"availabilitiesdata": [
{
"_id": "5b864fe68ab0b71f4f28021e",
"businessid": "5b7fb8559849fd485dc47240",
"userid": "5b714631faaae220d7cc07cf",
"hotel_filters": [
{
"_id": "5b864fe78ab0b71f4f28021f",
"hotelservice": [
{
"service_id": "5b472fb725556c3f02a875a8",
"service_name": "Parking",
"_id": "5b864fe78ab0b71f4f280223"
},
{
"service_id": "5b472fff25556c3f02a875aa",
"service_name": "Pool",
"_id": "5b864fe78ab0b71f4f280224"
}
],
"location_type": [
{
"locationtype_id": "5b18f4798c63f42019763b11",
"locationtype_name": "Quiet",
"_id": "5b864fe78ab0b71f4f280221"
},
{
"locationtype_id": "5b18f4e38c63f42019763b13",
"locationtype_name": "Central",
"_id": "5b864fe78ab0b71f4f280222"
}
],
"hotel_type": [
{
"hoteltype_id": "5b0813e2edefe23318834a27",
"hoteltype_name": "5 Star",
"_id": "5b864fe78ab0b71f4f280220"
}
]
}
]
},
{
"_id": "5b867766d63c4e2cdd5534d2",
"businessid": "5b7fb8559849fd485dc47240",
"userid": "5b714631faaae220d7cc07cf",
"hotel_filters": [
{
"_id": "5b867766d63c4e2cdd5534d3",
"hotelservice": [
{
"service_id": "5b472fff25556c3f02a875aa",
"service_name": "Pool",
"_id": "5b867767d63c4e2cdd5534d7"
},
{
"service_id": "5b472fdb25556c3f02a875a9",
"service_name": "AIR",
"_id": "5b867767d63c4e2cdd5534d8"
}
],
"location_type": [
{
"locationtype_id": "5b18f4d08c63f42019763b12",
"locationtype_name": "Scenic View",
"_id": "5b867767d63c4e2cdd5534d5"
},
{
"locationtype_id": "5b18f4e38c63f42019763b13",
"locationtype_name": "Central",
"_id": "5b867767d63c4e2cdd5534d6"
}
],
"hotel_type": [
{
"hoteltype_id": "5b081452edefe23318834a28",
"hoteltype_name": "3 Star",
"_id": "5b867767d63c4e2cdd5534d4"
}
]
}
]
}
]
}
]}
but what I want is something like this.
{
"status": true,
"msg": "Hotel Found",
"data": [
{
"_id": "5b714631faaae220d7cc07cf",
"name": "abc",
"surname": "xyz",
"email": "abc#gmail.com",
"hotel_data": {
"location": {
"type": "Point",
"coordinates": [
30.7052881,
76.84470799999997
]
},
"_id": "5b7fb8559849fd485dc47240",
"status": true,
"activityname": "Sparrow",
},
"availabilitiesdata": [
{
"_id": "5b864fe68ab0b71f4f28021e",
"businessid": "5b7fb8559849fd485dc47240",
"userid": "5b714631faaae220d7cc07cf",
"hotel_filters": [
{
"_id": "5b864fe78ab0b71f4f28021f",
"hotelservice": [
{
"service_id": "5b472fb725556c3f02a875a8",
"service_name": "Parking",
"_id": "5b864fe78ab0b71f4f280223"
},
{
"service_id": "5b472fff25556c3f02a875aa",
"service_name": "Pool",
"_id": "5b864fe78ab0b71f4f280224"
}
],
"location_type": [
{
"locationtype_id": "5b18f4798c63f42019763b11",
"locationtype_name": "Quiet",
"_id": "5b864fe78ab0b71f4f280221"
},
{
"locationtype_id": "5b18f4e38c63f42019763b13",
"locationtype_name": "Central",
"_id": "5b864fe78ab0b71f4f280222"
}
],
"hotel_type": [
{
"hoteltype_id": "5b0813e2edefe23318834a27",
"hoteltype_name": "5 Star",
"_id": "5b864fe78ab0b71f4f280220"
}
]
}
]
}
]
}
]}
I want only those availabilities which match the condition on my $lookup availability data. can someone help me out.
You can use $lookup pipeline variant to apply the $match inside the joined collection in 3.6
Something like ( replace $lookup & $match stage with below lookup pipeline )
{"$lookup":{
"from":"availabilities",
"let":{"hotel_data_id":"$hotel_data._id"},
"pipeline":[
{"$match":{
"hotel_filters.hotelservice.service_id":{"$in":["5b472fb725556c3f02a875a8"]},
"hotel_filters.location_type.locationtype_id":{"$in":["5b18f4798c63f42019763b11"]},
"$expr":{"$eq":["$$hotel_data_id","$businessid"]}
}}
],
"as":"availabilitiesdata"
}}