我需要在某个文档(按用户 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);
或者它有布局:
_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}
它仍然不起作用。我看过这个答案,但是他们引用了一个他们已经知道的值,比如名字?
catspeake
冉冉说
相关分类