[英]Why do I get a castError when I try to push an object to an array in MongoDB and Nodejs?
[英]I am trying to get a single array value from mongodb, but when i try i get the whole object
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
這是我的架構
1.我只想獲取我在Electro中創建的項目,我現在不想要整個對象。
var swapSchema = new mongoose.Schema({
swap: {
name: String,
Items: {
Electro: [
{
name: String,
info: String,
price: Number,
dateCreated: Date,
category: String,
id: Number
}
]
}
}
});
使用投影場:
如果要獲取所有數組:
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}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.