猫鼬查询子文档属性

时间:2017-01-25 21:05:13

标签: node.js mongodb azure-cosmosdb

我试图通过WEB API公开此功能。在此代码中设置的方式是有人在URL上执行GET以及以下形式提供查询字符串:

?field=value&anotherfield.subproperty=value

但我似乎无法根据子文档属性进行查询。下面你会找到我的准系统代码,记录我试图接收以及我的两个测试用例。

代码:

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var config = require('../config');
var User = require('../models/user');
var functions = require('../functions');

router.get('/', function(req,res,next) {
    //Check Permissions associated with UID *TODO
    var parameters = req.query;
    console.log(parameters);
    User.find(parameters, function(err, users) {
        if (err)
        {
            json = functions.generateOperationOutcome("exception","error",err,"exception");
            res.status(500);
            res.json(json);
        }
        else
        {
            //Check for blank result
            if (users.length === 0)
            {
                json = functions.generateOperationOutcome("not-found","warning","Non-Existent Resource","warning");
                res.status(404);
                res.json(json);
            }
            else {
                res.status(200);
                res.json(users);
            }
        }
    });
});

记录:

{
    "_id": "5871d2e814946a941d8611fb",
    "resourceType": "testResource",
    "link": [],
    "communication": [],
    "animal": {
      "genderStatus": {
        "coding": []
      },
      "breed": {
        "coding": []
      },
      "species": {
        "coding": []
      }
    },
    "contact": [],
    "photo": [],
    "maritalStatus": {
      "coding": []
    },
    "address": [],
    "gender": "unknown",
    "telecom": [
      {
        "system": "phone",
        "value": "2019196553",
        "use": "home"
      }
    ],
    "name": {
      "suffix": [],
      "prefix": [],
      "given": [],
      "family": []
    },
    "identifier": [
      {
        "use": "official",
        "type": {
          "coding": {
            "system": "kylec laptop",
            "version": "0.01",
            "code": "UDI",
            "display": "Universal Device Identifier",
            "userSelected": false
          },
          "text": "test"
        },
        "system": "test system",
        "value": "test value",
        "assigner": {
          "reference": "test assigner reference"
        },
        "period": {
          "start": "1992-12-31T09:59:59+00:00"
        }
      }
    ]
}

成功查询:

GET http://{{LOCAL}}/api/user?resourceType=testResource

从MongoDB返回这个模型。

不成功的查询(找不到与查询匹配的文档):

GET http://{{LOCAL}}/api/user?telecom.system=phone

不返回任何模型,结果为404。

1 个答案:

答案 0 :(得分:1)

您没有正确使用点符号,因为您正在搜索的属性位于数组中:

"telecom": [
  {
    "system": "phone",
    "value": "2019196553",
    "use": "home"
  }
]

查询数组内容通常需要您对阵列进行连接(对于DocumentDB查询),而不是简单的find()

如果你想在这里使用点符号,你需要创建一个子文档,如:

"telecom": {
    "system": "phone",
    "value": "2019196553",
    "use": "home"
  }

此时,您可以处理telecom.systemtelecom.valuetelecom.use.

等属性