【问题标题】:Meteor, MongoDB Manual Reference - 2 matching collection fieldsMeteor, MongoDB Manual Reference - 2个匹配的集合字段
【发布时间】:2015-08-21 03:50:17
【问题描述】:

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

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) 您确切知道要比较哪个字段,(2) 您确切知道要比较该字段的哪个值。
  • @DavidWeldon 我确切知道要比较哪个字段,但不一定知道该字段的值是否匹配。

标签: javascript mongodb meteor


【解决方案1】:

一方面,您没有正确指定字段子集,另一方面,即使您只指定一个字段,您也会在返回 中自动获得 _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});

【讨论】:

    猜你喜欢
    • 2020-12-10
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2015-07-24
    • 1970-01-01
    相关资源
    最近更新 更多