实例上的Mongoose key / val设置不在JSON或Console中显示..为什么?

时间:2022-03-20 04:51:52

I have some information on my mongoose models which is transient. For performance reasons I dont wish to store it against the model.. But I do want to be able to provide this information to clients that connect to my server and ask for it.

我有一些关于我的猫鼬模型的信息是瞬态的。出于性能原因,我不希望将其存储在模型中..但我确实希望能够将这些信息提供给连接到我的服务器并请求它的客户端。

Here's a simple example:

这是一个简单的例子:

 var mongoose = require('mongoose'),
     db = require('./dbconn').dbconn;

 var PersonSchema = new mongoose.Schema({
  name    : String,
  age     : Number,
});

var Person = db.model('Person', PersonSchema);
var fred = new Person({ name: 'fred', age: 100 });

The Person schema has two attributes that I want to store (name, and age).. This works.. and we see in the console:

Person模式有两个我想要存储的属性(名称和年龄)..这有效..我们在控制台中看到:

console.log(fred);

{ name: 'fred', age: 100, _id: 509edc9d8aafee8672000001 }

{name:'fred',年龄:100,_id:509edc9d8aafee8672000001}

I do however have one attribute ("status") that rapidly changes and I dont want to store this in the database.. but I do want to track it dynamically and provide it to clients so I add it onto the instance as a key/val pair.

但是,我确实有一个属性(“状态”)快速更改,我不想将其存储在数据库中..但我确实想要动态跟踪它并将其提供给客户端,所以我将它作为键添加到实例中/对。

fred.status = "alive";

If we look at fred in the console again after adding the "alive" key/val pair we again see fred, but his status isnt shown:

如果我们在添加“alive”键/ val对之后再次在控制台中查看fred,我们再次看到fred,但是他的状态未显示:

{ name: 'fred', age: 100, _id: 509edc9d8aafee8672000001 }

{name:'fred',年龄:100,_id:509edc9d8aafee8672000001}

Yet the key/val pair is definitely there.. we see that:

然而,键/值对确实存在..我们看到:

console.log(fred.status);

renders:

alive

The same is true of the JSON representation of the object that I'm sending to clients.. the "status" isnt included..

我发送给客户的对象的JSON表示也是如此...“状态”不包括在内。

I dont understand why.. can anyone help?

我不明白为什么..任何人都可以帮忙吗?

Or, alternatively, is there a better approach for adding attributes to mongoose schemas that aren't persisted to the database?

或者,是否有更好的方法将属性添加到未持久保存到数据库的mongoose模式?

2 个解决方案

#1


9  

Adding the following to your schema should do what you want:

将以下内容添加到您的架构应该做你想要的:

PersonSchema.virtual('status').get(function() {
  return this._status;
});

PersonSchema.virtual('status').set(function(status) {
  return this._status = status;
});

PersonSchema.set('toObject', {
  getters: true
});

This adds the virtual attribute status - it will not be persisted because it's a virtual. The last part is needed to make your console log output correctly. From the docs:

这会添加虚拟属性状态 - 它不会被保留,因为它是虚拟的。最后一部分是正确使控制台日志输出所必需的。来自文档:

To have all virtuals show up in your console.log output, set the toObject option to { getters: true }

要在console.log输出中显示所有虚拟,请将toObject选项设置为{getters:true}

Also note that you need to use an internal property name other than status (here I used _status). If you use the same name, you will enter an infinite recursive loop when executing a get.

另请注意,您需要使用status以外的内部属性名称(这里我使用了_status)。如果使用相同的名称,则在执行get时将进入无限递归循环。

#2


1  

Simply call .toObject() on the data object.

只需在数据对象上调用.toObject()即可。

For you code will be like:

你的代码就像:

fred.toObject()

#1


9  

Adding the following to your schema should do what you want:

将以下内容添加到您的架构应该做你想要的:

PersonSchema.virtual('status').get(function() {
  return this._status;
});

PersonSchema.virtual('status').set(function(status) {
  return this._status = status;
});

PersonSchema.set('toObject', {
  getters: true
});

This adds the virtual attribute status - it will not be persisted because it's a virtual. The last part is needed to make your console log output correctly. From the docs:

这会添加虚拟属性状态 - 它不会被保留,因为它是虚拟的。最后一部分是正确使控制台日志输出所必需的。来自文档:

To have all virtuals show up in your console.log output, set the toObject option to { getters: true }

要在console.log输出中显示所有虚拟,请将toObject选项设置为{getters:true}

Also note that you need to use an internal property name other than status (here I used _status). If you use the same name, you will enter an infinite recursive loop when executing a get.

另请注意,您需要使用status以外的内部属性名称(这里我使用了_status)。如果使用相同的名称,则在执行get时将进入无限递归循环。

#2


1  

Simply call .toObject() on the data object.

只需在数据对象上调用.toObject()即可。

For you code will be like:

你的代码就像:

fred.toObject()