How to find data on MongoDB by passing part of array objects - javascript

I tried to create an API for filtering the products by sending an array of objects as filters.
this is my Product schema:
const mongoose = require("mongoose");
const { s, rs, rn, rref, ref } = require("../utils/mongo");
let schema = new mongoose.Schema(
{
user: rref("user"),
name: rs,
description: s,
images: [s],
price: rn,
category: ref("category"),
filters: [
{
parent: ref("filter"),
value: s,
name: s,
},
],
subFilter: [
{
parent: s,
value: s,
title: s,
},
],
},
{ timestamps: true }
);
module.exports = mongoose.model("product", schema);
and this one is what I want to send as body to the API
{
category: '62445c3d922d127512867245'
filters: [
{ name: 'filter name 1', value: '62445c3d922d127512861236' },
{ name: 'filter name 2', value: '62445c3d922d127512861458' },
.....
]
}
as you see I want to filter my products based on category Id and an array of filter objects. I tried to write this query but it return an empty array.
this is my query:
filter: async (req, res) => {
try {
const { category, filters } = req.body;
const products = await Product.find({
category,
filters: {
$in: filters,
},
});
res.status(200).json(products);
} catch (err) {
res.status(500).json(err);
}
},
what stored on db
{
"_id" : ObjectId("62643acf19636d7db1804cb3"),
"images" : [
"image-1650735823476۸.jpg"
],
"user" : ObjectId("622606af0f40cb8ea37383dc"),
"name" : "شیر توپی 2 اینچ کلاس 150 پیشگام",
"description" : " برند پیشگام با مدارک و تاییدیه ",
"price" : NumberInt(5000000),
"category" : ObjectId("62445c4d922d127512867246"),
"filters" : [
{
"_id" : ObjectId("62643acf19636d7db1804cb4"),
"parent" : ObjectId("6264307f19636d7db1804b77"),
"value" : "626430bb19636d7db1804b78",
"name" : "Valve Type"
},
{
"_id" : ObjectId("62643acf19636d7db1804cb5"),
"parent" : ObjectId("6264319819636d7db1804b7b"),
"value" : "6264319819636d7db1804b7e",
"name" : "Body Type"
},
{
"_id" : ObjectId("62643acf19636d7db1804cb6"),
"parent" : ObjectId("626431ef19636d7db1804b82"),
"value" : "626431ef19636d7db1804b83",
"name" : "Bore Type"
},
{
"_id" : ObjectId("62643acf19636d7db1804cb7"),
"parent" : ObjectId("6264328519636d7db1804b85"),
"value" : "6264328519636d7db1804b86",
"name" : "Material Type"
},
{
"_id" : ObjectId("62643acf19636d7db1804cb8"),
"parent" : ObjectId("626435de19636d7db1804c10"),
"value" : "626439b619636d7db1804ca7",
"name" : "Trim Material"
},
{
"_id" : ObjectId("62643acf19636d7db1804cb9"),
"parent" : ObjectId("6264367919636d7db1804c17"),
"value" : "6264367919636d7db1804c18",
"name" : "End Conection"
},
{
"_id" : ObjectId("62643acf19636d7db1804cba"),
"parent" : ObjectId("626436a719636d7db1804c1f"),
"value" : "6264378119636d7db1804c28",
"name" : "Size"
},
{
"_id" : ObjectId("62643acf19636d7db1804cbb"),
"parent" : ObjectId("6264389219636d7db1804c6d"),
"value" : "6264389219636d7db1804c6f",
"name" : "Class / Pressure"
}
],
"subFilter" : [
{
"_id" : ObjectId("62643acf19636d7db1804cbc"),
"parent" : "6264328519636d7db1804b85",
"value" : "626433b919636d7db1804b93",
"title" : "Body Material"
}
],
"createdAt" : ISODate("2022-04-23T17:43:43.421+0000"),
"updatedAt" : ISODate("2022-04-23T17:53:29.016+0000"),
"__v" : NumberInt(0)
}

Consider this shrunk down set of inputs that capture the essence of the question. The comments "give away" what we are going to try to find and why. We only show one value for category because matching on that is trivial and not the interesting part of the query.
[
{
"category" : ObjectId("62445c4d922d127512867246"),
"filters" : [
// Matching Valve/value; include this doc
{"name" : "Valve", "value" : "626430bb19636d7db1804b78"},
// ALSO match Body/value; include this doc (but needs only 1 match)
{"name" : "Body", "value" : "6264319819636d7db1804b7e"}
]
}
,{
"category" : ObjectId("62445c4d922d127512867246"),
"filters" : [
// Not target value for Valve name (..79 instead of ...78):
{"name" : "Valve", "value" : "626430bb19636d7db1804b79"},
// ...but correct value for Body, so include this doc
{"name" : "Body", "value" : "6264319819636d7db1804b7e"}
]
}
,{
"category" : ObjectId("62445c4d922d127512867246"),
// No matching Valve or Body so this whole doc is ignored.
"filters" : [
{"name" : "Valve", "value" : "626430bb19636d7db1804b79"},
{"name" : "Body", "value" : "6264319819636d7db1804b7f"}
]
}
,{
"category" : ObjectId("62445c4d922d127512867246"),
// Not even name matches so ignore this too:
"filters" : [
{"name" : "Pipe", "value" : "6264319819636d7db1804eee"}
]
}
]
Assume also we set up inputs coming from the API like this, in their native form i.e. strings NOT ObjectId:
var targ_cat = '62445c4d922d127512867246';
var any_one_of = [
{ name: 'Valve', value: '626430bb19636d7db1804b78' },
{ name: 'Body', value: '6264319819636d7db1804b7e'}
];
We will use $filter as our main function but to do so, we must convert the incoming material into a form required by $filter.
// Convert inbound array of any_one_of into a something designed to work
// in the $filter function by comparing each name/value entry in the
// filters field to the item presented in $$this, meaning take:
// { name: 'Valve', value: '626430bb19636d7db1804b78' },
// and turn it into:
// {$and: [ {$eq:['Valve','$$this.name']}, {$eq:['62643...','$$this.value']} ] }
// Since any one of the entries is considered a hit, we package it all
// into an $or wrapper, not $and.
var or_list = [];
any_one_of.forEach(function(f) {
or_list.push( {$and: [
{$eq:[f['name'], '$$this.name']},
{$eq:[f['value'], '$$this.value']}
]});
});
var or_expr = {$or: or_list};
Now we are ready to query mongoDB:
db.foo.aggregate([
// Get this out of the way quickly; note we must make a new ObjectId!
{$match: {'category': new ObjectId(targ_cat)}}
// The interesting part of the query:
,{$addFields: {filters: {$filter: {input: '$filters', cond: or_expr}}}}
// Only keep those items where $filter found at least one of the
// targets:
,{$match: {$expr: {$gt:[{$size: '$filters'},0]} }}
]);

Related

Elastic Search delete a single element inside a field on _source

i`m trying to delete a element inside a field of my elastic field, current, I have this struct of data:
I need in my NODE application to delete a single element from produto_tags getting the id_produto_cor,
for example:
{
"_index" : "relatorio_recebimento_produto_hml",
"_type" : "_doc",
"_id" : "54XZ9HAB4DHa2O1nQlpk",
"_score" : 1.0,
"_source" : {
"id_usuario" : 1408,
"data_criacao" : "2020-03-19T22:10:40.465Z",
"produto_tags" : [
{
"id_produto_cor" : "2489664268",
"tags" : [ ]
},
{
"id_produto_cor" : "1000045010",
"tags" : [ ]
},
{
"id_produto_cor" : "1004600287",
"tags" : [ ]
},
{
"id_produto_cor" : "1032013410",
"tags" : [ ]
},
{
"id_produto_cor" : "2468436987",
"tags" : [ ]
}
],
"referencia_tags" : [ ],
"nome_relatorio" : "teste"
}
}
XDELETE
{
query: {
match: {
produto_tags.id_produto_cor: 489664268
}
}
}
I would expect my data to be like:
"_index" : "relatorio_recebimento_produto_hml",
"_type" : "_doc",
"_id" : "xYVD5XAB4DHa2O1ndFo1",
"_score" : 1.0,
"_source" : {
"id_usuario" : 1376,
"data_criacao" : "2020-03-16T21:32:46.369Z",
"produto_tags" : [
{
"id_produto_cor" : "1000045010",
"tags" : [ ]
},
{
"id_produto_cor" : "1004600287",
"tags" : [ ]
},
{
"id_produto_cor" : "1032013410",
"tags" : [ ]
},
{
"id_produto_cor" : "2468436987",
"tags" : [ ]
}
],
"referencia_tags" : { },
"nome_relatorio" : "teste"
}
},
...
This is my final structure
zzzzzzzzzzzzzzzzzzzz
zzzzzzzzzzzzzzzzzzzz
You can use _update_by_query
POST /index102/_update_by_query
{
"script":{
"source":"ctx._source.produto_tags.remove('1029831004')"
},
"query":{
"match_all":{} --> modify query to target specific documents
--> match_all will execute script on all documents
}
}
Query for changed structure:
iterate through produto_tags, match with specific value and store the matched object in an array (list). Then iteratate through list and remove the object from produto_tags
POST /<index_name>/_update_by_query
{
"script":{
"source":"if(ctx._source.produto_tags.size()==0) {return;} def list=[]; for(product in ctx._source.produto_tags) { if (product['id_produto_cor'] ==params.product_id){list.add(product)}} if(list.size()==0) return; for(d in list){ctx._source.produto_tags.remove(ctx._source.produto_tags.indexOf(d))}",
"params" : {
"product_id" : "2468436987"
}
},
"query":{
"match_all":{} --> to select specific documents
}
}

How to get slug value from array of objetcs if match with provide id?

I'm trying to get the slug value from the array if inside the object.extra.services array, one of the element makes match with the id im providing...
// Service ID provided
const serviceID = '5cdd7c55f5abb90a689a44be';
// Array of services Ids
getProductsServices(products) {
const productsServices = [
...new Set(products.map(product => product.extra.services))
];
const productsList = [].concat.apply([], productsServices);
return productsList;
},
// ServiceId Matching
serviceMatch(serviceID) {
return this.getProductsServices.includes(serviceID);
}
Now i need to get the slug value inside the array that match the service id provided.
products [{
"_id" : ObjectId("5e0257dcbe760674b10d4122"),
"desc" : "Diseño de Pagina Web",
"extra" : {
"image" : "/2018/06/diseño-de-logos-para-empresas.jpg",
"services" : [
"5cdd7c55f5abb90a689a44be",
"5cdd7c55f5abb90a689a3fcc",
"5cdd7c55f5abb90a689a3f42"
]
},
"name" : "Diseño de logo",
"slug" : "diseno-de-logotipos-online"
},
{
"_id" : ObjectId("5e0257dcbe760674b10d4122"),
"desc" : "Diseño de logo",
"extra" : {
"image" : "/2018/06/diseño-de-logos-para-empresas.jpg",
"services" : [
"5cdd7c55f5abb90a689a44be",
"5cdd7c55f5abb90a689a3fcc",
"5cdd7c55f5abb90a689a3f42"
]
},
"name" : "Diseño de logo",
"slug" : "diseno-de-logotipos-online"
},
{
"_id" : ObjectId("5e0257dcbe760674b10d4122"),
"desc" : "Diseño de Interior",
"extra" : {
"image" : "/2018/06/diseño-de-logos-para-empresas.jpg",
"services" : [
"5cdd7c55f5abb90a689a44be",
"5cdd7c55f5abb90a689a3fcc",
"5cdd7c55f5abb90a689a3f42"
]
},
"name" : "Diseño de logo",
"slug" : "diseno-de-logotipos-online"
}]
Try includes:
const found = products.find(product => product.extra.services.includes("5cdd7c55f5abb90a689a3fcc"))
if (found) {
console.log('found', found)
}
If I understood correctly, you can achieve it using find along with some:
const product = productsList.find(product => product.extra.services.some(id => id === serviceID))
console.log(product.slug)

MongoDB .find() only ObjectId's embedded in an Array.

I am trying to get only the ObjectId's from One specific Document that is embedded in the projects Array.
Basically I am trying to make a database that will have users and each user will have there own projects.
Thank you !
db.users.find().pretty()
{
"_id" : ObjectId("5762c0cf2b9a78006373a684"),
"name" : "seq",
"pass" : "seq",
"projects" : [
{
"pid" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},
{
"pid" : ObjectId("5762c0ba2b9a78006373a683"),
"name" : "bbb"
}
]
}
{
"_id" : ObjectId("5762c28d2b9a78006373a687"),
"name" : "leq",
"pass" : "leq",
"projects" : [
{
"pid" : ObjectId("5762c2892b9a78006373a685"),
"name" : "ccc"
},
{
"pid" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
]
}
let say we want two pids
{"pid" : ObjectId("5762c0ba2b9a78006373a682")} and
{"pid" : ObjectId("5762c2892b9a78006373a686"),}
and only inner documents
so required response should look like:
{
"_id" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},{
"_id" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
Aggregation framework can manipulate documents, match only needed ones and transform inner structure by project phase:
var match = {
$match : {
"projects.pid" : {
$in : [ObjectId("5762c0ba2b9a78006373a682"),
ObjectId("5762c2892b9a78006373a686")]
}
}
}
var unwind = {
$unwind : "$projects"
};
// now move array objet as top level object
var project = {
$project : {
_id : "$projects.pid",
name : "$projects.name",
// list other fields here
}
}
db.vic.aggregate([match, unwind, match, project])

Mongoose structure aggregation output without field names

I am using aggregate to paginate data from subdocuments. The subdocuments don't have a strict schema so they can be different for each document, this is making it difficult for me to structure my output as I don't know the field names for the subdocuments.
What I am using
Mongo 3.0.0
Node 0.10.33
Mongoose 3.9.7
My models
var BodySchema = new Schema({random: String},{strict:false});
var FeedSchema = new Schema({
name: String,
body:[BodySchema]
});
My data looks like this
[{
_id:"...",
name:"Power Rangers feed",
body:[
{
"_id":"...",
"name" : "Jason",
"colour" : "Red",
"animal" : "T-rex"
},
{
"_id":"...",
"name" : "Billy",
"colour" : "Blue",
"animal" : "Triceratops"
},
{
"_id":"...",
"name" : "Zach",
"colour" : "Black",
"animal" : "Mastadon"
}
]
},
{
_id:"...",
name:"Transformers feed",
body:[
{
"_id":"...",
"name" : "Optimus Prime",
"team" : "Autobots",
"class" : "leader"
"alt-mode" : "truck"
},
{
"_id":"...",
"name" : "Bumblebee",
"team" : "Autobots",
"class" : "scout"
"alt-mode" : "VW Beetle"
},
{
"_id":"...",
"name" : "Blaster",
"team" : "Autobots",
"class" : "Commmunicator"
"alt-mode" : "Sterio"
},
{
"_id":"...",
"name" : "Hotrod",
"team" : "Autobots",
"class" : "Warrior"
"alt-mode" : "Car"
}
]
}]
My current aggregate code looks like this
feed.aggregate([
{'$match':{_id:id('550234d3d06039d507d238d8')}},
{'$unwind':'$body'},
{'$skip':2},
{'$limit':2},
{
'$project': {
'_id':"$body._id",
'body': "$body"
}
},
], function(err, result){
if(err){return(res.send(500, err))}
res.send(result);
});
My current result looks like this
[{
_id:"...",
body:{
"_id":"...",
"name" : "Blaster",
"team" : "Autobots",
"class" : "Commmunicator"
"alt-mode" : "Sterio"
}
},
{
_id:"...",
body:{
"_id":"...",
"name" : "Hotrod",
"team" : "Autobots",
"class" : "Warrior"
"alt-mode" : "Car"
}
}]
My desired result looks like this
[
{
"_id":"...",
"name" : "Blaster",
"team" : "Autobots",
"class" : "Commmunicator"
"alt-mode" : "Sterio"
},
{
"_id":"...",
"name" : "Hotrod",
"team" : "Autobots",
"class" : "Warrior"
"alt-mode" : "Car"
}
]
My Question
How can I achieve my desired result structure.
So, you are going to hate this, but this is just how the $project and the $group stages in the aggregation pipeline work. You need to specify all of the fields that you want in the output "explicitly". Therefore:
feed.aggregate([
{'$match':{_id:id('550234d3d06039d507d238d8')}},
{'$unwind':'$body'},
{'$skip':2},
{'$limit':2},
{
'$project': {
'_id':"$body._id",
'name': '$body.name',
'team': '$body.team',
'class': '$body.alt-mode'
}
},
], function(err, result){
That really is the only way to do it.
There really is a strong reasoning behind that though, and most of the principles are backed up in SQL SELECT, with the exception of the * "wildcard" where applicable.
The general premise is that this is analogous to unix pipe in general operation so if you think like:
grep | awk | sed
Then the operations seem more logical in structure.

How to delete deep array in mongodb?

I am trying to delete "virtualNumber" : "12345" in the following document:
{
"_id" : ObjectId("50a9db5bdc7a04df06000005"),
"billingInfo" : null,
"date" : "dsfdsfsdfsd",
"description" : "sdfsdff",
"pbx" : {
"_id" : ObjectId("50a9db5bdc7a04df06000006"),
"did" : {
"1234567890" : {
"inventoryId" : "509df7547e84b25e18000001",
"didcountry" : "india",
"didState" : "bangalore",
"routeType" : "CallForward",
"didNumber" : "1234567890",
"didVirtualNumbers" : [
{
"virtualNumber" : "12345"
},
{
"virtualNumber" : "56789"
}
],
"id" : ObjectId("50a9db9acdfb4f9217000002")
}
},
},
I am using node.js, so I constructed a query in JavaScript:
var query = {_id: ObjectId("50a9db5bdc7a04df06000005")};
var obj = {};
obj["pbx.did.1234567890.didVirtualNumbers.virtualNumber"]=12345;
//problem
collection.update(query,{$pull:obj});
You need to match the array element like:
{"$pull": {"pbx.did.7259591220.didVirtualNumbers": {"virtualNumber": "12345"}}}
So you should change your code to:
obj["pbx.did.7259591220.didVirtualNumbers"]={"virtualNumber": "12345"};
Please refer to http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull
It mentions the pull field should be an array.

Categories