mongodb用mongoose取到的对象不能增加属性

先定义了一个article的schema

var mongoose = require(‘mongoose‘);
var Schema = mongoose.Schema;
exports.schema = new Schema({
    title: String,//标题
    description: String,//描述
    content: String,//内容
   status:{type: Number, defalut: 0}, //未发布:0 ,发布:1
   create_at: {type: Date, default: Date.now}//添加时间
});

增加一条测试数据:

var o = new articleModel();
o.title = ‘hello‘;
o.content = ‘这是一篇测试文章‘;
o.save(function(err,result){
    if(err){
        console.log(err.message);
    }
    console.log(result);
});

下面使用findOne方法获取这条记录,在获取到的记录上增加一个remark属性,并在控制台输出结果

articleModel.findOne({title: ‘hello‘}, function (err, article) {
    article.remark = ‘备注‘;
    console.log(err, article);
});
结果:{
  "content":"这是一篇测试文章",
  "title":"hello",
  "_id":"56f5ee83fcfad37f1371e952",
  "__v":0,
   "status":0,
  "create_at":"2016-03-26T02:05:55.814Z"
}

发现结果中remark属性没有显示,同时在schema中声明过的description也没有显示(因为增加数据的时候就没有设置description的值)。我现在把description也设置一个值看看查询结果:

articleModel.findOne({title: ‘hello‘}, function (err, article) {
    article.remark = ‘备注‘;
    article.description = ‘这是描述‘;
    console.log(err, article);
});
结果:{"description":"这是描述",
   "content":"这是一篇测试文章",
   "title":"hello",
   "_id":"56f5ee83fcfad37f1371e952",
   "__v":0,
   "status":0,
   "create_at":"2016-03-26T02:05:55.814Z"
 }

我们在发现description赋值成功,但是增加的remark属性还是无效。

这是为什么呢?因为Mongoose是個ODM (Object Document Mapper),类似于操作关系型数据库使用的ORM(Object Relational Mapper),我们使用Mongoose取到的数据的结构是要依赖于我们定义的schema结构的。增加的remark属性在schema中没有定义,所以我们在取到的结果中增加remark属性是无效的,而description属性先前在结构中有定义(不算新增),所以可以重新设置值。

结论:mongodb中使用mongoose取到的对象不能增加属性。

接着问题是,如果我需要在结果中补充新的属性使用怎么办?

方法1、在schema中直接增加需要补充的属性。

exports.schema = new Schema({
    title: String,//标题
    description: String,//描述
    content: String,//内容
    remark:String, //备注(补充新属性,现在和description一样了)
    create_at: {type: Date, default: Date.now}//添加时间
});

方法2、把查询到的结果clone一个对象,然后在新对象中补充属性。

articleModel.findOne({title: ‘hello‘}, function (err, article) {
  var newobj = null;
  if(article){
      newobj = {
        _id:article._id,
        title: article.title,//标题
        description: article.description,//描述
        content: article.content,//内容
        remark:"备注",
        create_at: article.create_at,
     status: article.status,
     status_name: article.status==1?‘发布‘:‘未发布‘;
    	};
  }
    console.log(newobj);
});

方法3:像上面的例子在schema中已经有了status表示状态,如果我们仅仅需要一个status_name显示文章状态的中文解释。不要clone新对象的方式,可以使用schema的虚拟属性。

声明修改一下如下:
var mongoose = require(‘mongoose‘);
var Schema = mongoose.Schema;
var schema = new Schema({
    title: String,//标题
    description: String,//描述
    content: String,//内容
    status: {type: Number, defalut: 0}, //未发布:0 ,发布:1
    create_at: {type: Date, default: Date.now}//添加时间
});
schema.virtual(‘status_name‘).get(function () {
    return this.status == 1 ? ‘发布‘ : ‘未发布‘;
});
exports.schema = schema;

查询到结果后直接用可以直接使用status_name属性:
articleModel.findOne({title: ‘hello‘}, function (err, article) {
    console.log(article.stauts_name);
});
时间: 2024-08-29 02:05:00

mongodb用mongoose取到的对象不能增加属性的相关文章

mongodb用mongoose得到的对象不能增加属性解决

一,先定义了一个goods(商品)的models var mongoose = require('mongoose'); var Schema = mongoose.Schema; var productSchema = new Schema({ "productId":String, "producName": String, "salePrice":Number, "productImage":String }); mod

php 给对象动态增加属性

<?php error_reporting(-1); ini_set('display_errors','on'); class A { public $a = 'hello'; public function add() { $this->b = 'world'; }- public static function p() { echo 'world',PHP_EOL; }- } $a = new A; $a->add(); $a->c = 'test'; $a->p();

mongodb用mongoose查库的对象,不能增加属性

node + koa2 + mongodb 写了一个给前端的接口 如果不是写这个接口,这辈子都发现不了mongodb里这个大坑 mongoose 是个ODM(Object Document Mapper),mongodb是nosql数据库,文档存储 mysql,sqlserver,oracle都是关系型数据库 所以mongodb无法在取到对象增加属性,必须在追加时候重新用一个对象,或者在schema中添加这个对象的key model.js // 这里用来建数据库表结构相关的 const mong

mongoDB (mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

MongoDB - 简介 官网:https://www.mongodb.com/ MongoDB 是一个基于分布式文件存储的数据库,由 C++ 语言编写,旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. MongoDB - 安装及运行 下载 07/05/2017 Current Stable Release (3.4.6) https://www.mongodb.com/dow

初学mongodb和mongoose

进入安装mongodb的文件夹下的bin文件夹,执行命令:mongo ,就可以使用mongodb了. 安装好mongoose后,在app.js中,首先引入mongoose这个模块: var mongoose = require('mongoose'); //连接到对应的数据库: //端口号默认链接27017: //test是数据库的名称,mongodb不需要建立数据库,当你需要连接的数据库不存在时,会自动创建一个: mongoose.connect('mongodb://localhost/te

前端知识点回顾——mongodb和mongoose模块

mongodb和mongoose模块 数据库 数据库有关系型数据库(MySQL)和非关系型数据库(mongodb),两者的语法和数据存储形式不一样. mySQL 关系型数据库 类似于表格的形式,每一条数据都是以id为标识 table thead name sex age id tbody May female 18 1 Simple male 25 2 mongodb 非关系型数据库 集合,相当于表的概念,Bson,一条数据代表一个文档(数据的基本单位) { id : name : May ag

【重学Node.js 第3篇】mongodb以及mongoose的使用

mongodb以及mongoose的使用 本篇为这个系列的第三篇,想看更多可以直接去github的项目:https://github.com/hellozhangran/happy-egg-server 更多介绍可以看:https://www.cnblogs.com/zhangran/p/11963616.html mongodb mongodb是典型的非关系型数据库,关于它的背景介绍和优势劣势这里就不赘述,我们直接讲实操或者说最有可能用到的部分. mongodb常用指令 mongod --co

ajax回调函数中使用$(this)取不到对象的解决方法

如果在ajax的回调函数内使用$(this)的话,实践证明,是取不到任何对象的,需要的朋友可以参考下 $(".derek").each(function(){ $(this).click(function(){ var params = $(this).parent().serialize(); var obj=$(this).parent().siblings("div#caskContent"); var form=$(this).parent(); $.aja

spring data mongodb中,如果对象中的属性不想加入到数据库字段中

spring data mongodb中,如果对象中的属性不想加入到数据库字段中,可加@Transient注解,声明为透明属性 spring data mongodb 官网帮助文档 http://www.boyunjian.com/javadoc/org.springframework.data/spring-data-mongodb/1.2.3.RELEASE/_/org/springframework/data/mongodb/core/query/Criteria.html#all(jav