2017-01-25 202 views
1

我想通過一個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

你沒有正確使用點符號,作爲屬性你尋求的是一個數組中:

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

查詢陣列的內容通常會要求你做一個加入對數組(DocumentDB查詢),而不是一個簡單的find()

如果你想在這裏用點符號的能力,你需要創建一個子文檔,如:

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

在這一點上,你能夠解決諸如telecom.system性質, telecom.valuetelecom.use.

+0

謝謝你的回答。你能詳細說明我們如何利用像我們這樣的數組的查詢嗎?我們目前正在利用DocumentDB和MongoDB支持,所以我們希望有MongoDB/MongoDB方法來完成這項工作。 – Kyle

+0

在純MongoDB中,您可以像[this]一樣執行此操作(https://docs.mongodb.com/manual/tutorial/query-documents/#match-a-field-without-specifying-array-index) –