Javascript Mongoose,更新时属性没有返回足够的值
我的模式:Javascript Mongoose,更新时属性没有返回足够的值,javascript,mongoose,Javascript,Mongoose,我的模式: ... var images = new Schema({ name: String, poster: { w: { type: Number, default: null }, h: { type: Number, default: null }, local: { type: Str
...
var images = new Schema({
name: String,
poster: {
w: { type: Number, default: null },
h: { type: Number, default: null },
local: { type: String, default: null },
local2: { type: Object, default: null }
}
})
...
当我创建db.images.create({name:123})
时,它返回:
{
_id: <something object>,
name: 123,
poster: {
w: null,
h: null,
local: null,
local2: null
}
}
{
_id:,
姓名:123,
海报:{
w:空,
h:空,
本地:空,
local2:null
}
}
这真的很好,但当我尝试更新时
db.images.updateOne({u id:},{poster:{w:10})
它只是返回:
{
_id: <something object>,
name: 123,
poster: {
w: 10
}
}
{
_id:,
姓名:123,
海报:{
w:10
}
}
我想阻止poster中的所有字段,如何做到这一点?您可以使用选择({poster:0})和updateOne()
db.images.updateOne({ _id: <id> }, { $set: { 'poster.w': 10 } })
db.images.updateOne({u id:},{$set:{'poster.w':10})
我想你理解了我的问题。我试图理解你的问题!我理解的想法是,你不想在更新后张贴海报。我想用对象ex:{w:10}更新海报,但doc中的旧对象是{w:8,h:10},它只是自动更新w并保留值。实际上,问题是你使用的是updateOne(),它只返回一个响应对象为true,1你应该使用findOneAndUpdate()返回当前更新对象的响应。