我在编写查询以使用SerialNumber
从数组中提取对象时遇到问题db.query.findOne({orderId:'13052017002593'});
{
"_id" : "36ndPwFKGdcTDRERx",
"orderId" : "13052017002593",
"items" : [
{
"serialNumber" : "XXX",
"status" : "COMPLETED",
"shippingType" : "MESSENGER",
"shippingDate" : ISODate("2017-05-16T10:16:16.941+0000"),
"returnDeposit" : true
},
{
"serialNumber" : "YYY",
"status" : "COMPLETED",
"shippingType" : "MESSENGER",
"shippingDate" : ISODate("2017-05-16T10:16:16.941+0000"),
"returnDeposit" : true
}
],
"createdAt" : ISODate("2017-05-13T10:15:09.904+0000")
}
假设我想提取对象
{
"serialNumber" : "XXX",
"status" : "COMPLETED",
"shippingType" : "MESSENGER",
"shippingDate" : ISODate("2017-05-16T10:16:16.941+0000"),
"returnDeposit" : true
},
我如何仅提取此对象?
我有数据库3.2;
我尝试运行这些查询:
let params = {
$and: [{ orderId }, {'items.serialNumber':serialNumber}]
};
let ctx = {
fields:{items:{$elemMatch:serialNumber}}
}
let order = Orders.find(params,ctx).fetch()
哪个给出错误;
我也尝试过:
let order = Orders.find({orderId:orderId},{_id:0,items:{$elemMatch: {serialNumber:serialNumber}}});
返回订单对象中的所有值
任何人都可以帮助我。任何建议都非常感谢!谢谢!
我也在流星论坛上发帖 https://forums.meteor.com/t/how-to-get-only-one-object-from-array-of-objects/43613