【发布时间】:2016-07-14 12:16:02
【问题描述】:
我正在尝试从产品没有费率的产品数组中删除所有产品。在下面的查询中,我尝试检查 rate 数组的长度,但是 它们似乎都不起作用。任何帮助表示赞赏。
提前致谢
var ProductRateSchema = new Schema({
product: {
type: Schema.ObjectId,
ref: 'products'
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
rates: [{
type: Schema.ObjectId,
ref: 'rates'
}]
});
var InventorySchema = new Schema({
name: {
type: String,
default: '',
required: 'Please enter in a name',
trim: true
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
products: [productRateSchema]
});
var inventoryId = req.body.inventoryId;
var productId = req.body.productId;
// none of these queries work
db.inventory.findOneAndUpdate({ '_id': inventoryId,
{$pull: {'products': { product: productId, 'rates': { $eq:[] }}}}, function (err, result) {
});
db.inventory.findOneAndUpdate({ '_id': inventoryId,
{$pull: {'products': { product: productId, 'rates': {$size: {$lt: 1}}}}}, function (err, result) {
});
db.inventory.findOneAndUpdate({ '_id': inventoryId,
{$pull: {'products': { product: productId, 'rates': null }}}, function (err, result) {
});
【问题讨论】:
-
什么查询?这里只有模式定义。
-
查询在那里,没有滚动条,但如果你滚动会有更多代码
-
错过了他们。并不重要,因为所有尝试失败都有充分的理由。主要用于无效语法,因为在每种情况下您都错过了单独的“查询”和“更新”参数。还是回答了。
标签: node.js mongodb mongoose mongodb-query