假设我有以下架构:
const mySchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
date: Number,
data: {
field1 : Number,
field2 : Number
}
});
我想用具有“ myAwesomeDate”的文档用“ myAwesomeValue”更新field2。我当前在async / await函数中的代码是:
// V1
var myAwesomeDocument = await myModel.findOneAndUpdate(
{date: myAwesomeDate}, //selector
{$set: { //update
'data.field2': myAwesomeValue
}},
{ //options
upsert: false,
new: true
}
).exec();
此代码使我可以处理更新的文档。
如果我没记错的话,以下代码具有相同的行为,但应避免,因为它首先将文档加载到客户端(因此效率较低)(Mongoose difference between .save() and using update()):
// V2
var myAwesomeDocument = await myModel.findOne({date: myAwesomeDate}).exec();
myAwesomeDocument.data.field2 = myAwesomeValue;
myAwesomeDocument = await myAwesomeDocument.save().exec();
现在,我想使用.doSomething()方式使代码更具可读性:
// V3 (any code mistake here ?)
var myAwesomeDocument = await myModel.findOne({date: myAwesomeDate})
.set(
{'data.field2': myAwesomeValue},
{upsert: false, new: true}
)
.exec();
我的问题首先是效率,然后是代码的可读性:
感谢任何回答!
答案 0 :(得分:1)
从您提供的示例中,最有效的是v1
V3的正确版本为以下版本:
instance = await model
.findOneAndUpdate({date})
.set({'data.f2': f1},)
.setOptions({new: true})
.exec();
说明: 猫鼬findOneAndUpdate返回Query(请参见示例)。然后,我们使用查询的方法来设置更新操作和选项。
最后,您可以使用V1或我提供的更新的V3,因为它们在后台使用相同的数据库调用。
您始终可以使用mongoose.set('debug': true)分析实际将哪些查询发送到数据库。
要备份我上面所说的内容,这是我用来运行测试的代码片段。您可以像这样调用它:
const uri = 'mongodb://localhost:27017/test-sav';
const mongoose = require('mongoose');
const bombardCount = process.argv[2] ? parseInt(process.argv[2]) : 1;
const debug = process.argv[3] === 'true';
const date = 1234567;
const f1 = 1;
const f2 = 2;
let model;
(async function () {
await mongoose.connect(uri, {useNewUrlParser: true, useUnifiedTopology: true});
const schema = new mongoose.Schema({
date: Number,
data: {
f1: Number,
f2: Number,
}
});
model = mongoose.model('model', schema);
console.log('### START ###')
const doc1 = await bombard(v1, bombardCount);
console.log(doc1);
const doc2 = await bombard(v2, bombardCount);
console.log(doc2);
const doc3 = await bombard(v3, bombardCount);
console.log(doc3);
const doc4 = await bombard(v4, bombardCount);
console.log(doc4);
console.log('### END ###');
})().catch(error => console.error(error)).then(() => process.exit(1));
async function v1() {
console.log('### V1 ###\n');
await beforeEach();
console.time('### V1 ###');
let instance = await model.findOneAndUpdate(
{date},
{
$set: {
'data.f2': f1,
},
},
{
upsert: false,
new: true
}
).exec();
console.timeEnd('### V1 ###');
await afterEach();
return instance;
}
async function v2() {
console.log('### V2 ###\n');
await beforeEach();
console.time('### V2 ###');
let instance = await model.findOne({date}).exec();
instance.data.f2 = f1;
instance = await instance.save();
console.timeEnd('### V2 ###');
await afterEach();
return instance;
}
async function v3() {
console.log('### V3 ###\n');
console.time('### V3 ###');
await beforeEach();
let instance = await model
.findOne({date})
.set(
{'data.f2': f1},
{upsert: false, new: true}
).exec();
console.timeEnd('### V3 ###');
await afterEach();
return instance
}
async function v4() {
console.log('### V4 ###\n');
console.time('### V4 ###');
await beforeEach();
let instance = await model
.findOneAndUpdate({date})
.set({'data.f2': f1})
.setOptions({new: true})
.exec();
console.timeEnd('### V4 ###');
await afterEach();
return instance;
}
async function beforeEach() {
await new model({
date,
data: {
f1,
f2,
},
}).save();
mongoose.set('debug', debug);
}
async function afterEach() {
mongoose.set('debug', debug);
await model.deleteMany({});
}
async function bombard(f, times) {
let x;
for (let i = 0; i < times; i++) {
x = await f();
}
return x;
}
node index.js [repeats?=number] [debug?=true/false]