使用水线时无法将项目推入文档中的 MongoDB 数组
Unable to push an item into a MongoDB array within the document while using Waterline
我正在努力处理更新调用,但由于某种原因它似乎不起作用。对于一些快速上下文,我们有一个 Node.js 应用程序 运行ning on Sails.js with Waterline ORM.
根据我的 package.json 文件,以下是我使用的版本:
- connect-mongo: ^1.3.2
- mongodb: ^2.2.29
- 风帆:~0.12.4
- sails-mongo: ^0.12.3
我有一个名为 "products" 的 collection,每个产品在数据库中看起来都是这样的:
{
"_id" : ObjectId("59d5f12025423dc0261c911d"),
"category" : ObjectId("59a9bcf984d756998eaa22e5"),
"status" : "pendingReview",
"isDeleted" : false,
"events" : [
{
"when" : 1507193120,
"actor" : "56cc0f76e1a25cde0d2c15ab",
"action" : "Submitted product",
"note" : "Jeff added this product. It is awaiting review."
}
],
"productId" : "171005-00000",
"createdAt" : ISODate("2017-10-05T08:45:20.538Z"),
"updatedAt" : ISODate("2017-10-05T08:45:20.538Z")
}
我有一个 UI,用户可以在其中 "approve" 多个产品,然后再将它们显示给网站访问者。为此,我只想通过将 status
键更改为 "approved" 并在每个文档的 events
数组中添加一个事件来更新多个记录。该事件必须位于数组的位置 0。我正在尝试使用以下代码更新这些记录,但它似乎不起作用:
var moment = require('moment');
Products.native(function(error, collection) {
if (error) {
throw error;
}
collection.update(
{ _id: ['59d5f12025423dc0261c911d'] },
{
$set: {
status: 'approved',
$push: {
events: {
when: moment().unix(),
actor: req.body.userId,
action: 'Approved product',
note: req.body.userName + ' approved this product.'
}
}
}
},
{ multi: true },
function(error, count, status) {
if (error) {
sails.log.error(error);
return res.serverError('Database error. Reference ID: ' + req.referenceId);
}
return res.ok(count);
});
});
当我 运行 这个查询时,我没有得到任何错误,当我检查我的数据库时,记录没有被更新。当我 运行 查询时,我得到以下数据:
{
"ok": 1,
"nModified": 0,
"n": 0
}
这是怎么回事?为什么不更新?如果我理解正确,查询能够找到文档,但它没有得到更新。那是对的吗?如果是,我该如何解决?谢谢。
$push 应该和$set 处于同一级别。他们都是运营商...
这意味着你应该这样改变:
{
$set: {
status: 'approved'
},
$push: {
events: {
when: moment().unix(),
actor: req.body.userId,
action: 'Approved product',
note: req.body.userName + ' approved this product.'
}
}
}
我正在努力处理更新调用,但由于某种原因它似乎不起作用。对于一些快速上下文,我们有一个 Node.js 应用程序 运行ning on Sails.js with Waterline ORM.
根据我的 package.json 文件,以下是我使用的版本:
- connect-mongo: ^1.3.2
- mongodb: ^2.2.29
- 风帆:~0.12.4
- sails-mongo: ^0.12.3
我有一个名为 "products" 的 collection,每个产品在数据库中看起来都是这样的:
{
"_id" : ObjectId("59d5f12025423dc0261c911d"),
"category" : ObjectId("59a9bcf984d756998eaa22e5"),
"status" : "pendingReview",
"isDeleted" : false,
"events" : [
{
"when" : 1507193120,
"actor" : "56cc0f76e1a25cde0d2c15ab",
"action" : "Submitted product",
"note" : "Jeff added this product. It is awaiting review."
}
],
"productId" : "171005-00000",
"createdAt" : ISODate("2017-10-05T08:45:20.538Z"),
"updatedAt" : ISODate("2017-10-05T08:45:20.538Z")
}
我有一个 UI,用户可以在其中 "approve" 多个产品,然后再将它们显示给网站访问者。为此,我只想通过将 status
键更改为 "approved" 并在每个文档的 events
数组中添加一个事件来更新多个记录。该事件必须位于数组的位置 0。我正在尝试使用以下代码更新这些记录,但它似乎不起作用:
var moment = require('moment');
Products.native(function(error, collection) {
if (error) {
throw error;
}
collection.update(
{ _id: ['59d5f12025423dc0261c911d'] },
{
$set: {
status: 'approved',
$push: {
events: {
when: moment().unix(),
actor: req.body.userId,
action: 'Approved product',
note: req.body.userName + ' approved this product.'
}
}
}
},
{ multi: true },
function(error, count, status) {
if (error) {
sails.log.error(error);
return res.serverError('Database error. Reference ID: ' + req.referenceId);
}
return res.ok(count);
});
});
当我 运行 这个查询时,我没有得到任何错误,当我检查我的数据库时,记录没有被更新。当我 运行 查询时,我得到以下数据:
{
"ok": 1,
"nModified": 0,
"n": 0
}
这是怎么回事?为什么不更新?如果我理解正确,查询能够找到文档,但它没有得到更新。那是对的吗?如果是,我该如何解决?谢谢。
$push 应该和$set 处于同一级别。他们都是运营商...
这意味着你应该这样改变:
{
$set: {
status: 'approved'
},
$push: {
events: {
when: moment().unix(),
actor: req.body.userId,
action: 'Approved product',
note: req.body.userName + ' approved this product.'
}
}
}