在mongoose上查找查询执行,响应数据为多个对象,真实数据在_doc属性或字段中,它只出现在某些场景中。我可以通过获取Obj._doc.something来处理数据,但是我无法编辑数据并保存(mongoose模型函数)。请帮我解决这个问题。
注意:架构的字段已动态添加。
PatientOrderMigration.find({ mrn: orderitem.mrn, visituid: orderitem.visituid },
function (err, orderDoc)
{
//log data correctly.
console.log(orderDoc);
// undefined
console.log(orderDoc._id);
// correct data
console.log(orderDoc._doc._id);
}
答案 0 :(得分:2)
我知道这已经很老了,但是我遇到了类似的问题。要解决此问题,请使用.lean()。
lean选项告诉Mongoose跳过对结果文档进行水化处理。这样可以加快查询速度并减少占用的内存,但是结果文档是普通的旧JavaScript对象(POJO),而不是Mongoose文档。
因此您的查询将是:
PatientOrderMigration.find({ mrn: orderitem.mrn, visituid: orderitem.visituid }).lean()
答案 1 :(得分:0)
好吧,Model.find()
会为您提供在数据库中找到的对象数组,如果您想直接访问您的对象,可以使用Model.findOne()
OR =>快速修复:
PatientOrderMigration.find({ mrn: orderitem.mrn, visituid: orderitem.visituid },function (err, orderDoc) {
orderDoc = orderDoc[0];//Here is the Fix, you can comment this if you use findOne
orderDoc.mrn = "New Value you want to update";
orderDoc.save(function(err, result){
console.log('err',err)
})
}}
答案 2 :(得分:0)
起源问题是 find
方法返回一个数组,这是由接受的答案指出的。但是标题“Mongoose return data inside _doc object”与另一个常见错误有关。所以我想在这里继续我的研究。
当您使用 Mongoose API 查询数据(find、findOne、findById ..)时,Mongoose 会在响应中为您提供一个 Mongoose Document 类的实例,这与您的不同Javascript 对象。
您有一些选项可以获取您的 Javascript 对象,如文档中所述:
我创建了一个测试项目来演示这些方法,请随意测试:
const mongoose = require('mongoose');
// connect to database
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true, useUnifiedTopology: true });
// define the schema
const kittySchema = new mongoose.Schema({
name: String
// this flag indicate that the shema we defined is not fixed,
// document in database can have some fields that are not defined in the schema
// which is very likely
}, { strict: false });
// compile schema to model
const Kitten = mongoose.model('Kitten', kittySchema);
test();
async function test() {
// test data
const dataObject = { name: "Kitty 1", color: "red" };
const firstKitty = new Kitten(dataObject); // attribute color is not defined in the schema
// save in database
firstKitty.save();
// find the kitty from database
// mongoose return a document object, which is different from our data object
const firstKittyDocument = await Kitten.findOne({ name: "Kitty 1" });
console.log("Mongoose document. _id :", firstKittyDocument._id); // _id of document
console.log("Mongoose document. name :", firstKittyDocument.name); // "Kitty 1"
console.log("Mongoose document. color :", firstKittyDocument.color); // undefined
// --> the document contains _id and other fields that we defined in the schema
// we can call the method .toObject to get the plain object
console.log("Using .toObject() method. _id :", firstKittyDocument.toObject()._id); // _id of document
console.log("Using .toObject() method. name :", firstKittyDocument.toObject().name); // "Kitty 1"
console.log("Using .toObject() method. color :", firstKittyDocument.toObject().color); // "red"
// --> Using .toObject() method, we get all the fields we have in the dataObject
// or we can use lean method to get the plain old javascript object
const firstKittyPOJO = await Kitten.findOne({ name: "Kitty 1" }).lean();
console.log("Using .lean() method. _id :", firstKittyPOJO._id); // _id of document
console.log("Using .lean() method. name :", firstKittyPOJO.name); // "Kitty 1"
console.log("Using .lean() method. color :", firstKittyPOJO.color); //"red"
// --> Using .lean() method, we get all the fields we have in the dataObject
}