【发布时间】:2018-08-21 07:01:19
【问题描述】:
这是我的帐户架构:
const account = new mongoose.Schema({
name: String,
age: Number,
coins: Number,
phone: Number,
sex: {
type: String,
default: 'f'
}
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
}
)
然后我想更改查找数据:
Account.find({age: {$eq: 20}}, function(err, data) {
console.log(err, data);
data[0].testword = 'hello'; //can`t add element
data[0].age = 'world'; //can`t change num to string
console.log(data)
})
结果:
[ { _id: 5a97667011998b05027b9e62,
name: 'bob',
age: 20,
sex: 'f' }]
[ { _id: 5a97667011998b05027b9e62,
name: 'bob',
age: 20,
sex: 'f' }]
所以我该怎么做才能得到这个结果:
[ { _id: 5a97667011998b05027b9e62,
name: 'bob',
age: 'world', //change type
testword: 'hello', //add an attribute
sex: 'f' }]
也许我可以分配给一个新对象,但如果我的架构有很多属性?
【问题讨论】: