Meteor,MongoDB手册参考 - 2个匹配的收集字段

时间:2015-08-20 17:39:48

标签: javascript mongodb meteor

我正在尝试确定两个集合是否有匹配的字段。我的收藏品看起来像这样:

Collection1.find().fetch();

_id: 'W6YB9M7FtKRdJHxgr',
postId: '1052732379946708491_1086374506',
voters: [ [Object] ],
totalVotes: 0

Collection2.find().fetch();

{
  {
  _id: 'ZEht9cYHemQ32Pkk7',
  postId: '1052732379946708491_1086374506',
  firstName: 'Frank',
  lastName: 'User'
  },
  {
  _id: 'KQpt8rYHemQ328aB',
  postId: '1052770843237994691_1864563700',
  firstName: 'Joe',
  lastName: 'UserTwo'
  }
}

我正在尝试检查collection1.postId是否等于collection2.postId中的某个文档来决定要对哪些文档执行操作。

var a = collection1.findOne({},{postId: 1});
var b = collection2.findOne({postId: a});

不返回字段匹配的集合。我的想法就是我可以说:

if(b) {
  collection2.update({...});
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

您一方面没有正确指定字段子集,另一方面,即使您只指定了一个字段,您也会在返回中自动获取 _id 对象。尝试:

var a = collection1.findOne({},{fields: {postId: 1}}).postId; // a will now be a string instead of an object
var b = collection2.findOne({postId: a});