goofansu
V2EX  ›  问与答

用mongo的同学,请问在一个document里要查询另外一个document怎么弄?

  •  
  •   goofansu · Dec 12, 2013 · 2558 views
    This topic created in 4547 days ago, the information mentioned may be changed or developed.
    情况如下:

    1. A有个bid对应B的id,
    2. 查询A的时候想获取关联的B,拿B里面的其他字段。

    场景:

    我是在做统计,统计结果要用B里面一个字段作为描述,现在只能先把A统计完,再到B里面按bid一个个拿。

    有没有先进点的方法?
    2 replies    1970-01-01 08:00:00 +08:00
    SevenJ
        1
    SevenJ  
       Dec 12, 2013
    如果是用mongoose的话, schema定义里头可以
    bid: ObjectId, ref: 'B'
    query时加populate(‘字段X')
    goofansu
        2
    goofansu  
    OP
       Dec 12, 2013
    @SevenJ 额,纯shell

    你的这个做法是不是就等于是在A里面定义了一个B的ref?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   945 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:02 · PVG 05:02 · LAX 14:02 · JFK 17:02
    ♥ Do have faith in what you're doing.