我正在使用mongoose,这是我的订单架构:
var orderSchema = new Schema({
user: { type : Schema.Types.ObjectId, ref: 'User', required : true },
note: { type : String },
paidWithCredit: { type : Boolean, default : false },
createdOn: { type : Date, default : Date.now },
products : { type: [
{
product: { type : Schema.Types.ObjectId, ref: 'Product', required : true },
taken: { type : Boolean, default: false},
paid: { type : Boolean, default: false}
}
], required: true }
}, {collection: 'Orders'});
以下是订单文档的示例:
{
"_id": "59f08ed17c13831fa8837176",
"user": "59f079f15d021c2ab8d96185",
"products": [
{
"product": "59eb7fa3c6cc101434b4ced4",
"_id": "59f08ed17c13831fa883717f",
"paid": false,
"taken": false
},
{
"product": "59eb7f8fc6cc101434b4ced3",
"_id": "59f08ed17c13831fa883717e",
"paid": false,
"taken": false
},
{
"product": "59eb7f75c6cc101434b4ced2",
"_id": "59f08ed17c13831fa883717d",
"paid": false,
"taken": false
},
{
"product": "59eb7f63c6cc101434b4ced1",
"_id": "59f08ed17c13831fa883717c",
"paid": false,
"taken": false
},
{
"product": "59eb7f51c6cc101434b4ced0",
"_id": "59f08ed17c13831fa883717b",
"paid": false,
"taken": false
},
{
"product": "59eb7f03c6cc101434b4cecf",
"_id": "59f08ed17c13831fa883717a",
"paid": false,
"taken": false
},
{
"product": "59eb7eefc6cc101434b4cece",
"_id": "59f08ed17c13831fa8837179",
"paid": false,
"taken": false
},
{
"product": "59eb7e57c6cc101434b4cecb",
"_id": "59f08ed17c13831fa8837178",
"paid": false,
"taken": false
},
{
"product": "59eb7d29ca88db092874d2c4",
"_id": "59f08ed17c13831fa8837177",
"paid": false,
"taken": false
}
],
"createdOn": "2017-10-25T13:17:05.218Z",
"paidWithCredit": false,
"__v": 0
}
我要查找包含特定产品或一系列产品中的一种产品的订单。 我试着像这样查询:
{
"products": {
"$in": [
{
"_id": "59f08ed17c13831fa883717f",
"paid": false,
"taken": false
}
]
}
}
甚至像这样简单的查询:
{
"products": {
"product": "59f08ed17c13831fa883717f"
}
}
我正在努力的事情都没有。 我只想查询Orders集合中包含“products”属性中包含给定Product ids数组的至少一个id的Orders。