2016-11-30 53 views
0

1.I不要在电阵列的项目,但整个文档我想从MongoDB的一个数组值,但是当我尝试,我得到整个对象

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 

这是我的Schema
1.我试图让我在Electro中创建的项目,我不想让我现在得到的整个对象。

var swapSchema = new mongoose.Schema({ 
swap: { 
    name: String, 
    Items: { 
     Electro: [ 
      { 
       name: String, 
       info: String, 
       price: Number, 
       dateCreated: Date, 
       category: String, 
       id: Number 
       } 
      ] 
     } 
    } 
}); 

回答

1

使用投影领域:如果你想获得的所有阵列

dbswap.findOne(
    { 'swap.Items.Electro.id':data.id}, 
    { 'swap.Items.Electro' : 1} 
    , function(err, obj){ 

将返回类似:

{ 
_id: ObjectId("sdfsdfsdf"), 
Electro:[{....},{....}] 
} 

或者,如果你想只有数组中匹配查询的对象:

dbswap.findOne(
    { 'swap.Items.Electro.id':data.id}, 
    { 'swap.Items.Electro.$' : 1} 
    , function(err, obj){ 

将返回类似:

{ 
_id: ObjectId("sdfsdfsdf"), 
Electro:{your match object} 
} 
+0

非常感谢你!!!! – Pentico

相关问题