[英]I am trying to get a single array value from mongodb, but when i try i get the whole object
1.I Don't get the Item in Electro Array but the whole doc 1.我没有得到电子阵列中的项目,但整个文档
getItem(data){
dbswap.findOne(
{ 'swap.Items.Electro.id':data.id,
'swap.Items.Electro.id':data.id }, function(err,item){
if(err){
return (err);
}
if(item){
console.log(item);
}
});
} // EOF
This is my Schema
这是我的架构
1.I am trying to get the item i create in Electro only, I don't want the whole object i am getting at the moment.1.我只想获取我在Electro中创建的项目,我现在不想要整个对象。
var swapSchema = new mongoose.Schema({
swap: {
name: String,
Items: {
Electro: [
{
name: String,
info: String,
price: Number,
dateCreated: Date,
category: String,
id: Number
}
]
}
}
});
Use the projection field : 使用投影场:
If you want to get all the array : 如果要获取所有数组:
dbswap.findOne(
{ 'swap.Items.Electro.id':data.id},
{ 'swap.Items.Electro' : 1}
, function(err, obj){
will return something like : 将返回类似:
{
_id: ObjectId("sdfsdfsdf"),
Electro:[{....},{....}]
}
Or if you want only the object in the array who match the query : 或者,如果只希望数组中与查询匹配的对象:
dbswap.findOne(
{ 'swap.Items.Electro.id':data.id},
{ 'swap.Items.Electro.$' : 1}
, function(err, obj){
will return something like : 将返回类似:
{
_id: ObjectId("sdfsdfsdf"),
Electro:{your match object}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.