猫鼬如何通过findOne在文档中获取嵌套对象

时间:2020-10-06 17:54:51

标签: javascript node.js mongodb mongoose mongodb-query

我需要在某个文档(通过用户ID搜索)中获取一个嵌套对象,该文档中还包含一个对象(无法保证该对象将是同一对象)。

我的用户模型为:

const mongoose = require('mongoose');
const { bool } = require('@hapi/joi');

const monitoringSchema = new mongoose.Schema({
    type: Object,
    default: {}
})

const hubSchema = new mongoose.Schema({
    hubID: {
        type: String,
        default: ""
    },
    isSetup: {
        type: Boolean,
        default: false
    },
    monitoring: {
        type: monitoringSchema
    }
}, {strict:false})

const finalUserSchema = new mongoose.Schema({
    username: {
        type: String,
        required: true,
        max: 255
    },
    email: {
        type: String,
        required: true,
        max: 255,
    },
    password: {
        type: String,
        required: true,
        min: 10,
        max: 1024,
    },
    date: {
        type: Date,
        default: Date.now
    },
    isVerified: {
        type: Boolean,
        default: false
    },
    hub: {
        type: hubSchema
    }
},  {strict:false});

module.exports = mongoose.model('User', finalUserSchema);

OR具有以下布局:

_id: "id"
isVerified: true
username: "nathan"
email: "email@email.com"
hub:
    hubID: "id"
    monitoring: // WHOLE OBJECT I NEED TO RETREIVE
        exampleObject: 
            exampleValue: exampleKey

我有一个需要更新的用户ID数组,并且尝试了查询:

for(i in usersToUpdate){
        User.findOne({_id: usersToUpdate[i], "hub.monitoring": {}}, {}, callbackResponse);
        function callbackResponse(err, data){
            if(err) return console.log(err)
            console.log(data)
        }
    }

但是它返回null作为数据,因此显然查询是错误的。我知道错误是:

{_id: usersToUpdate[i], "hub.monitoring": {}}

更具体地说:

"hub.monitoring": {}

我正在使用{}来引用监视中的对象,引用未知对象并获取其值(如通配符)的正确引用是什么?我尝试过:

 {_id: usersToUpdate[i], "hub.monitoring": Object}

,它仍然不起作用。我见过this answer,但是他们引用了他们已经知道的值,例如名称?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用findOne的2对象形式,其中第一个对象是查询,第二个对象是要返回的内容的投影。

User.findOne({_id: usersToUpdate[i]}, {"hub.monitoring": {$exists: true}}, callbackResponse);
function callbackResponse(err, data){
    if(err) return console.log(err)
    console.log(data)
}

这样,如果监视对象存在,则将返回该对象。

答案 1 :(得分:0)

要仅检索monitoring对象,可以使用aggregation管道。

使用$match进行过滤,并使用$project进行输出/抑制字段。

User.aggregate([
  {
    $match: {
      _id: mongoose.Types.ObjectId(usersToUpdate[i]),
    },
  },
  {
    $project: {
      monitoring: "$hub.monitoring",
      _id: 0,
    },
  },
]).exec(callbackResponse); 

Playground example

相关问题