【问题标题】:Meteor MongoDB return single objectMeteor MongoDB 返回单个对象
【发布时间】:2017-12-04 05:06:48
【问题描述】:
{
    "_id" : "R9zbu8oTWwgxT5eCR",
    "practiceSetup" : {
        "operatoriesSettings" : [ 
            {
                "name" : "Operatory 1",
                "description" : "Room 1",
                "status" : true
            },
                            {
                "name" : "Operatory 2",
                "description" : "Room B",
                "status" : true
            }
        ],  

}

我正在尝试查找名称为“Operatory 1”的对象,并且只返回该对象。

这是我尝试过的,

Practice.findOne({ _id: 'R9zbu8oTWwgxT5eCR' }, { "$elemMatch": { "practiceSetup.operatoriesSettings": { "name": "Operatory 1" } } } );

但是,这将返回整个文档,而不是那个特定的对象,不确定我在这里做错了什么。

【问题讨论】:

标签: mongodb meteor


【解决方案1】:

你可以试试这个,

Practice.findOne({ _id: 'R9zbu8oTWwgxT5eCR' }).map(function(u){
        return u.practiceSetup
                .operatoriesSettings
                .filter( m => m.name == "Operatory 1");
    })

这将返回:

[
 [ 
    {
        "name" : "Operatory 1",
        "description" : "Room 1",
        "status" : true
    }
 ]
]

【讨论】:

    【解决方案2】:

    $elemMatch 投影就是这样工作的:

    $elemMatch 运算符将查询结果中的字段内容限制为仅包含与 $elemMatch 条件匹配的第一个元素。

    如果你只需要那个特定的子文档,你可以使用 JS:

    const doc = Practice.findOne(
      { _id: 'R9zbu8oTWwgxT5eCR' },
      {
        $elemMatch: {
          'practiceSetup.operatoriesSettings': {
            name: 'Operatory 1'
          }
        }
      }
    );
    
    const operator1 = doc.practiceSetup.operatoriesSettings[0];
    

    【讨论】:

      【解决方案3】:

      我发现使用“Projections”会从数组中返回匹配的对象。

      查询:

      { "_id": "R9zbu8oTWwgxT5eCR", "practiceSetup.operatoriesSettings": { $elemMatch: {'name': 'Operatory 1'} } }
      

      投影

      {"practiceSetup.operatoriesSettings.$.name": 1}
      

      完整查询:

      db.Practice.findOne({ 
          "_id" : "R9zbu8oTWwgxT5eCR", 
          "practiceSetup.operatoriesSettings" : {
              "$elemMatch" : {
                  "name" : "Operatory 1"
              }
          }
      }, { 
          "practiceSetup.operatoriesSettings.$.name" : 1.0
      });
      

      返回

      { 
          "_id" : "R9zbu8oTWwgxT5eCR", 
          "practiceSetup" : {
              "operatoriesSettings" : [
                  {
                      "name" : "Operatory 1", 
                      "description" : "Room 1", 
                      "status" : true
                  }
              ]
          }
      }
      

      流星

      使用 Meteor 方法时,您需要在使用 Projections 时使用“fields”。

         return Practice.findOne({
              "_id": "R9zbu8oTWwgxT5eCR",
              "practiceSetup.operatoriesSettings": {
                "$elemMatch": {
                  "name": data
                }
              }
            }, 
              { fields: { 'practiceSetup.operatoriesSettings.$.name': 1, _id: 0 } }
             );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-09
        • 1970-01-01
        • 2011-10-01
        相关资源
        最近更新 更多