简体   繁体   English

如何在Mongoose查询中找到缺少的架构字段

[英]How do I find missing schema fields on a Mongoose query

If I add new fields directly to my MongoDB database and I forget to add them to my Mongoose schema, how can I alert myself to the problem without it failing silently. 如果我将新字段直接添加到MongoDB数据库中而忘记将它们添加到Mongoose模式中,那么如何在不提示失败的情况下提醒自己注意该问题。

The following example shows that all fields are returned from a query (regardless of the schema) but undefined if you access the key. 下面的示例显示,所有字段都是从查询返回的(与模式无关),但是如果您访问键,则未定义。

var mongoose = require('mongoose');

var user_conn = mongoose.createConnection('mongodb://db/user');
var userSchema = mongoose.Schema({
  email: String,
  // location: String,
  admin: Boolean
});
var User = user_conn.model('User', userSchema);

User.findOne({email: 'foo@bar.com.au'}, function (err, doc) {
  if (err) return console.log(err);
  console.log(doc);
  console.log(doc.email);
  console.log(doc.location);
});

Result: 结果:

{ _id: 57ce17800c6b25d4139d1f95,
  email: 'foo@bar.com.au',
  location: 'Australia',
  admin: true,
  __v: 0 }      // <-- console.log(doc);

foo@bar.com.au  // <-- console.log(doc.email);

undefined       // <-- console.log(doc.location);

I could read each doc key and throw an error if undefined, but is this the only way? 我可以读取每个文档密钥,如果未定义,则会引发错误,但这是唯一的方法吗?

Versions Node.js: 6.5.0 Mongoose: 4.6.0 版本 Node.js:6.5.0猫鼬:4.6.0

You can set strict to false on the schema so it will save all properties even if they are not in schema: 您可以在模式上将strict设置为false ,这样即使它们不在模式中,它也会保存所有属性:

 var userSchema = mongoose.Schema({ email: String, admin: Boolean }, {strict: false}); 

http://mongoosejs.com/docs/guide.html#strict http://mongoosejs.com/docs/guide.html#strict

In order to get a property which is not in schema you need to use doc.toObject() and use the returned object, or to use doc.get('location') 为了获得不在架构中的属性,您需要使用doc.toObject()并使用返回的对象,或者使用doc.get('location')

Following on from Amiram's answer. 接下来是Amiram的回答。 I now use lean() to get the object during a query but when I need to update or save it still looks up the schema. 现在,我在查询期间使用lean()获取对象,但是当我需要更新或保存它时,仍然会查询架构。

User.findOne({email: 'foo@bar.com.au'}).lean().exec(function (err, doc) {
  console.log(doc);
  console.log(doc.email);
  console.log(doc.location);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Mongoose中执行查找查询? - How do I perform a find query in Mongoose? 如何在 mongoose 中查询模式中的数组? - How do I query an array within a schema in mongoose? 如何在 mongoose 架构中查找和填充引用 object 字段 - How to find and populate reference object fields in a mongoose schema 如何在mongoose查询.find()中调用remove()? - How do I call remove() within mongoose query .find()? 我如何在猫鼬中引用架构的 id - How do I refer the id of schema in mongoose 如何动态修改猫鼬模式,我必须添加未在初始猫鼬模式中定义的动态字段 - How to modify mongoose schema dynamically, i have to add dynamic fields that are not defined in initial mongoose Schema 设置 ZCCADDEDB567ABAE643E15DCF0974E503Z 模式进行身份验证后,如何使用 Mongodb 查询数据库? - How do I query the database using Mongodb after I set up a Mongoose schema for authentication? 优化-在Mongoose MongoDB模式中的所有字段上查找 - Optimization - find on all fields in Mongoose MongoDB schema 如何使用 mongoose's.find() 通过在模式中找到的数组中搜索名称或项目来返回用户? - How do I use mongoose's .find() to get a user returned by searching for a name or an item in an array that is found in a schema? Mongoose查询缺少(或未定义)字段的位置 - Mongoose query where with missing (or undefined) fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM