元素匹配返回所有数据,而我只需要选择的数据 [英] Elem match is returning all the data whereas I need only the selected data
问题描述
db.mdb.collection('product_master').find({'project_code':'usha-fos', 'product_details' : {$elemMatch:{'Division':'Electric Fans'}}})
.limit(1000,function(err,results){
if(!err){
//console.log("projection succeeded");
//console.log("its working");
//console.log(results);
if(results.length==0){
console.log("length is 0")
}
app.send(req,res,results);//this will send the appropriate data to my controller
}
else{
//console.log("it is throwing an error")
}
})
我的JSON格式数据如下:
And my data in JSON format is as follows:
{
"_id" : ObjectId("57df7f5ab7079b1ab4d8a7dc"),
"agency_code" : "v5global",
"client_code" : "USHA_FOS",
"project_code" : "usha-fos",
"product_details" : [
{
"Division" : "Anything",
"Category" : "Ceiling Fans",
"Subcategory1" : "CF - UNIVERSAL - SMART",
"Subcategory2" : "STELLA",
"SKU" : "1200MM STELLA WH CF",
"SKU_Code" : "111048511",
"Description" : "",
"is_active" : "true"
},
{
"Division" : "Electric Fans",
"Category" : "Ceiling Fans",
"Subcategory1" : "CF - ADMIRE - UNDER LIGHT",
"Subcategory2" : "FONTANA",
"SKU" : "1280MM FONTANA ORCHID GOLD IVORY CF",
"SKU_Code" : "111055447",
"Description" : "",
"is_active" : "true"
},
{
"Division" : "AC&R",
"Category" : "Ceiling Fans",
"Subcategory1" : "CF - ADMIRE - UNDER LIGHT",
"Subcategory2" : "FONTANA",
"SKU" : "1250MM FONTANA MAPLE ANTIQUE BRASS CF",
"SKU_Code" : "111055448",
"Description" : "",
"is_active" : "true"
},
{
"Division" : "Electric Fans",
"Category" : "Ceiling Fans",
"Subcategory1" : "CF - ADMIRE - UNDER LIGHT",
"Subcategory2" : "FONTANA",
"SKU" : "1230MM FONTANA LOTUS BLK CHROME CF",
"SKU_Code" : "111056414",
"Description" : "",
"is_active" : "true"
},
{
"Division" : "Home Comfort",
"Category" : "Ceiling Fans",
"Subcategory1" : "CF - ADMIRE - UNDER LIGHT",
"Subcategory2" : "FONTANA",
"SKU" : "1230MM FONTANA LOTUS STEEL CF",
"SKU_Code" : "111056430",
"Description" : "",
"is_active" : "true"
}
]}
/*我想要划分为电风扇的数据,但是无论划分如何,我都将获取所有数据. 请帮我*/ /谢谢./
/* I want the data whose division is electric fans but I am getting all the data irrespective of the division. Please help me out */ /Thanks in advance./
推荐答案
$elemmatch(query) returns all rows in a array when there is atleast one row matching the query criteria.
$ elemMatch(projection) 用作投影时,仅返回所有匹配行的第一行.
$elemMatch(projection) returns only the first row of all the matching rows when used as projection.
您可以使用数组聚合运算符 $ filter .
You can easily get all matching rows by using array aggregation operator $filter.
在这种情况下,将根据传递的crtieria过滤产品详细信息.
In this scenario, product details will be filtered based on the crtieria passed.
aggregate([{
$match: {
"project_code": 'usha-fos'
}
}, {
$project: {
"agency_code": 1,
"client_code": 1,
"project_code": 1,
"product_details": {
"$filter": {
"input": "$product_details",
"as": "result",
cond: {
$eq: ["$$result.Division", "Electric Fans"]
}
}
}
}
}])
这篇关于元素匹配返回所有数据,而我只需要选择的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!