Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
阵列中的MongoDB推送记录_Mongodb_Meteor - Fatal编程技术网

阵列中的MongoDB推送记录

阵列中的MongoDB推送记录,mongodb,meteor,Mongodb,Meteor,我有一个userAccounts Meteor Mongo数据库,其中存储了用户喜欢的用户名和帖子。这就是它的样子: userAccounts.insert({ username:Meteor.user().username, likedPosts:{ postId:[this._id], createdAt:new Date() } }); 我希望每次用户喜欢另一篇文章时,都将该文章添加到likedPosts中的postId中。所以我做了这

我有一个userAccounts Meteor Mongo数据库,其中存储了用户喜欢的用户名和帖子。这就是它的样子:

userAccounts.insert({
    username:Meteor.user().username,
    likedPosts:{
      postId:[this._id],
      createdAt:new Date()
    }
  });
我希望每次用户喜欢另一篇文章时,都将该文章添加到likedPosts中的postId中。所以我做了这样的事情:

userAccounts.update(
    Meteor.user().username,{
      $push:{
        'likedPosts':{
          'postId':this._id,
          'createdAt':new Date()
        }}
});

但由于某些原因,它不会将新的post id推送到数组,它只保留上面插入的第一条记录,因此插入工作正常。知道我做错了什么吗?提前谢谢

问题可能出在选择器上。当
update
remove
将单个值作为选择器时,他们假定该值是
\u id

而不是:

userAccounts.update({
    Meteor.user().username,{
      $push:{
        'likedPosts':{
          'postId':this._id,
          'createdAt':new Date()
        }}
});
尝试:

此外,当您插入时:

userAccounts.insert({
    username:Meteor.user().username,
    likedPosts:{
      postId:[this._id],
      createdAt:new Date()
    }
  });
likedPosts
被初始化为一个对象,而不是长度为1的数组。所以你不能推它。改为:

userAccounts.insert({
    username:Meteor.user().username,
    likedPosts: [{
      postId: [this._id],
      createdAt: new Date()
    }]
  });

问题可能出在选择器上。当
update
remove
将单个值作为选择器时,他们假定该值是
\u id

而不是:

userAccounts.update({
    Meteor.user().username,{
      $push:{
        'likedPosts':{
          'postId':this._id,
          'createdAt':new Date()
        }}
});
尝试:

此外,当您插入时:

userAccounts.insert({
    username:Meteor.user().username,
    likedPosts:{
      postId:[this._id],
      createdAt:new Date()
    }
  });
likedPosts
被初始化为一个对象,而不是长度为1的数组。所以你不能推它。改为:

userAccounts.insert({
    username:Meteor.user().username,
    likedPosts: [{
      postId: [this._id],
      createdAt: new Date()
    }]
  });
这是您使用的地方,您实际上也有一个日期操作:

userAccounts.update(
  Meteor.user().username,
  {
   '$push':{ 'likedPosts.postId': this._id }
   '$set': { 'likedPosts.createdAt':new Date() }
  }
);
一个是“附加到已创建的数组”,另一个是“设置新日期”

我觉得这个命名有点不对劲,也许你的意思是
“updatedAt”

这就是你使用的地方,你实际上也有一个日期操作:

userAccounts.update(
  Meteor.user().username,
  {
   '$push':{ 'likedPosts.postId': this._id }
   '$set': { 'likedPosts.createdAt':new Date() }
  }
);
一个是“附加到已创建的数组”,另一个是“设置新日期”


我觉得这个名字有点不对劲,也许你的意思是这里的“updatedAt”。

它似乎没有什么不同。它似乎没有什么不同。