对子文档的猫鼬查询使用投影返回其他子文档的数组

时间:2019-01-12 12:29:35

标签: mongodb express mongoose

更新:我正在寻找一个适用于mongodb投影的答案:https://docs.mongodb.com/manual/reference/method/db.collection.findOne/#definition

我正在尝试使用投影过滤子文档上的查询,以使其仅返回特定数组。但是,当过滤结果时,还包括另一个子文档的数组。当我不过滤时,它仅返回找到的文档。

我尝试了包括和排除位置元素在内的其他过滤选项,但无法获得理想的回报。

猫鼬模式

const stationSchema = new mongoose.Schema({

  mac: String,
  stationName: String,
  syncReadings: Boolean,
  temperature: Array,
  humidity: Array,
  measures: [{
  date: Date,
  temperature: Number,
  humidity: Number
  }],
  lastUpdated: Date

});

// Define user schema
var userSchema = mongoose.Schema({

    local            : {
        email        : String,
        password     : String
    },
    facebook         : {
        id           : String,
        token        : String,
        name         : String,
        email        : String
    },
    twitter          : {
        id           : String,
        token        : String,
        displayName  : String,
        username     : String
   },
   google           : {
        id           : String,
        token        : String,
        email        : String,
        name         : String
   },
   apiKey: String,
   stations : [stationSchema]

   },  
   {
       usePushEach: true 
   }
);

Api处理程序

app.get('/api/stations/:stationName/measures', function(req, res, next) {

  var user = {
    apiKey: req.user.apiKey
  }

  const query = {
        apiKey: user.apiKey,
        stations.stationName': req.params.stationName
  }

  const options = {
        'stations.measures': 1
  }

  User.findOne(query, options)
  .exec()
  .then(stations => {       
        res.status(200).send(stations)
  })
  .catch(err => {
    console.log(err);
    res.status(400).send(err);
  })

});

一个用户下有两个电台:

[
    {
        "_id": "5c39c99356bbf002fb092ce9",
        "stations": [
            {
                "stationName": "livingroom",
                "mac": "5C:CF:7F:77:12:FB",
                "_id": "5c39c9ab56bbf002fb092cea",
                "lastUpdated": "2019-01-12T11:07:01.802Z",
                "syncReadings": false,
                "measures": [],
                "humidity": [],
                "temperature": [
                    {
                        "date": "2019-01-12T11:07:01.802Z",
                        "temperature": "20"
                    }
                ]
            },
            {
                "stationName": "office",
                "mac": "5C:CF:7F:77:12:FC",
                "_id": "5c39cacdce4ac903123f0150",
                "measures": [],
                "humidity": [],
                "temperature": []
            }
        ]
    }
]

API调用

http://localhost:8080/api/stations/livingroom/measures

结果

{
    "_id": "5c39c99356bbf002fb092ce9",
    "stations": [
        {
            "measures": []
        },
        {
            "measures": []
        }
    ]
}

尝试了投影选项

 const options = {
   'stations.measures': 1
 }

 const options = {
   'stations.$.measures': 1
 }

 const options = {
   'stations.$': 1,
   'stations.$.measures': 1
 }

 const options = {
   'stations.$': 1,
   'stations.measures': 1
 }

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

仅尝试使用这些查询参数,然后您将获得具有所请求站点的用户。

var user = {
    apiKey: req.user.apiKey
  }

  const query = {
        apiKey: user.apiKey,
        'stations.stationName': req.params.stationName
  }

然后执行此操作

    User.findOne(query, options)
      .exec()
      .then(stations => {      

        for(let station of stations){
           if(station.measures[1]){ // here it is the index  

           res.status(200).send(stations);
        }
    }


})
  .catch(err => {
    console.log(err);
    res.status(400).send(err);
  }) 

实际上,在猫鼬中,您无法使用此方法查询子文档。您只能像完成操作一样查询子文档

相关问题