How to filter the response from mongoDB, so nested arrays will include only items that meet a condition? - node.js

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

Related

mongodb update value in 3 level nested array of objects

I want to update flag in array of objects which are 3 levels deep nested.
I have this array of ages:
[20, 25]
I want to set flag: 1 for those users which has type: students and ages: 20 or 25
Here is my Users collection:
{
"_id": "wef324DGSshf",
"userTypes": [
{
"type": "students",
"users": [
{
"name": "John",
"age": 20,
"flag": 0
},
{
"name": "Mike",
"age": 25,
"flag": 0
}
]
},
{
"type": "students",
"users": [
{
"name": "Henry",
"age": 40,
"flag": 0
},
{
"name": "Nick",
"age": 25,
"flag": 0
}
]
},
{
"type": "teachers",
"users": [
{
"name": "Kim",
"age": 20,
"flag": 0
},
{
"name": "Charles",
"age": 25,
"flag": 0
}
]
}
]
}
After update I expect this response:
{
"_id": "wef324DGSshf",
"userTypes": [
{
"type": "students",
"users": [
{
"name": "John",
"age": 20,
"flag": 1
},
{
"name": "Mike",
"age": 25,
"flag": 1
}
]
},
{
"type": "students",
"users": [
{
"name": "Henry",
"age": 40,
"flag": 0
},
{
"name": "Nick",
"age": 25,
"flag": 1
}
]
},
{
"type": "teachers",
"users": [
{
"name": "Kim",
"age": 20,
"flag": 0
},
{
"name": "Charles",
"age": 25,
"flag": 0
}
]
}
]
}
What I have tried so far is this query:
Users.update(
{ "userTypes.users.age": { $in: [20,25] } },
{ "$set": { "userTypes.users.$.flag": "1" } }
);
My solution is not updating the desired records, can anyone help me with the right query for this case?

How to set up aggregation pipeline for specific use case in MongoDB

`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.`

Typescript to update Mongodb

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
});
}
});

Joining only matching elements

I want to join more than two collections in MongoDB . Is it possible to join?
I have 2 collections and I want to join them based by their ids
news collection
{
"lang": "ru",
"news": [{
"title": "title",
"content": "desc",
"image": "path",
"categoryId": {
"$oid": "6171499e0ba3d75082823c9a"
},
"createdAt": {
"$date": "2021-10-21T12:30:33.215Z"
},
"_id": {
"$oid": "61715d7990905adefcf314d1"
}
}]
}
and I have got newscategory collection like that
[
{
"lang": "ru",
"categories": [{
"name": "sdfsdsdcsdf",
"_id": {
"$oid": "6171499e0ba3d75082823c9a"
}
}]
},
{
"lang": "en",
"categories": [{
"name": "ooo",
"_id": {
"$oid": "61712980f8ee795c6a569dcb"
}
}, {
"name": "yuy",
"_id": {
"$oid": "61712980f8ee795c6a569dcc"
}
}, {
"name": "rtyrty",
"_id": {
"$oid": "61712980f8ee795c6a569dcd"
}
}]
}
]
Expected Output:
[
{
"_id": "61715d7990905adefcf314d0",
"lang": "ru",
"news": [
{
"title": "sdf",
"content": "sdf",
"image": "asdas",
"categoryId": "6171499e0ba3d75082823c9a",
"caregory": [
{
"name": "sdfsdsdcsdf",
"_id": "6171499e0ba3d75082823c9a"
},
]
"createdAt": "2021-10-21T12:30:33.215Z",
"_id": "61715d7990905adefcf314d1"
}
],
Match is based on both "category_id" and "categories._id".
How can I achieve result like that ?

Retrieve object information within an array with mongoose

I'm making an API Rest with node, express, typescript and mongoose. I have a method GET that return this result:
{
"success": true,
"status": 200,
"message": "categories listed",
"data": [
{
"_id": "612650e55fe1ce0de138e2af",
"products": [
{
"_id": "612650e55fe1ce0de138e2b0",
"productID": {
"reviews": [
"611e61ba8cb43f7454787ebb",
"611e62008cb43f7454787ebc"
],
"_id": "610b18f3e2244a187b36f2d7",
"title": "PS4",
"description": "La mejor consola del mercado del mundo, mundial",
"photo": "https://amazon-clone-jparrot.s3.amazonaws.com/1628123519052",
"price": 400,
"stockQuantity": 23,
"__v": 0,
"category": "60fc6454b68717acc239cc6a",
"owner": "610b9ed8763da4308223aae0",
"averageRating": null,
"id": "610b18f3e2244a187b36f2d7"
},
"quantity": 1,
"price": 400
}
],
"owner": {
"_id": "611d2d39dfcc705972c1ccb8",
"name": "Jaume",
"email": "jaumeparrot2#gmail.com",
"password": "$2a$10$Rv9Rzrff6578feCdDjyeKuarKCSHYRqKp5n5wTi2IWtcLBOupvPgu",
"__v": 0,
"address": "611e9ccdf47c7a7a9cb1d5d9"
},
"estimatedDelivery": "Wednesday September 1st",
"__v": 0
}
]
}
The problem is that I need to retrieve the object "owner", that is, I need to recover this json:
{
"success": true,
"status": 200,
"message": "categories listed",
"data": [
{
"_id": "612650e55fe1ce0de138e2af",
"products": [
{
"_id": "612650e55fe1ce0de138e2b0",
"productID": {
"reviews": [
"611e61ba8cb43f7454787ebb",
"611e62008cb43f7454787ebc"
],
"_id": "610b18f3e2244a187b36f2d7",
"title": "PS4",
"description": "La mejor consola del mercado del mundo, mundial",
"photo": "https://amazon-clone-jparrot.s3.amazonaws.com/1628123519052",
"price": 400,
"stockQuantity": 23,
"__v": 0,
"category": "60fc6454b68717acc239cc6a",
"owner": {
"_id": "611d2d39dfcc705972c1ccb8",
"name": "Jaume",
"about": "My na is Jaume",
"__v": 0
},
"averageRating": null,
"id": "610b18f3e2244a187b36f2d7"
},
"quantity": 1,
"price": 400
}
],
"owner": {
"_id": "611d2d39dfcc705972c1ccb8",
"name": "Jaume",
"email": "jaumeparrot2#gmail.com",
"password": "$2a$10$Rv9Rzrff6578feCdDjyeKuarKCSHYRqKp5n5wTi2IWtcLBOupvPgu",
"__v": 0,
"address": "611e9ccdf47c7a7a9cb1d5d9"
},
"estimatedDelivery": "Wednesday September 1st",
"__v": 0
}
]
}
For generate this JSON, I'm using this method:
https://github.com/jparrot92/amazon-clone-back/blob/master/src/controllers/order.ts
To retrieve you should use data.owner. This will give owner details as object.
This is the solution:
const products = await Order.find({ owner: req.user._id })
.populate('owner')
.populate({
path: 'products.productID',
populate: {
path: 'owner',
model: 'Owner',
},
})
.exec();

Resources