MongoDB objects taken with mongoose cannot add attributes

Source: Internet
Author: User
Tags add time findone

A article schema is defined first.

var mongoose = require (' Mongoose '); var Schema = Mongoose.   Schema;exports.schema = new Schema ({    title:string,//title    description:string,//Description    content:string,//content Status:{type:number, defalut:0},//unpublished: 0, posted: 1 create_at: {type:date, default:date.now}//add Time});

Add a test data:

var o = new Articlemodel (); o.title = ' Hello '; o.content = ' This is a test article '; O.save (function (err,result) {    if (err) {        Console.log (err.message);    }    Console.log (result);});

Use the FindOne method to get this record, add a remark property to the record obtained, and output the result in the console

Articlemodel.findone ({title: ' Hello '}, Function (err, article) {    Article.remark = ' remark ';    Console.log (err, article);}); Result: {"content": "This is a test article", "title": "Hello", "_id": "56f5ee83fcfad37f1371e952", "__v": 0,   "status": 0, "Create_at": "2016-03-26t02:05:55.814z"}

The remark attribute in the discovery result is not displayed, and the description declared in the schema is not displayed (because the value of description is not set when the data is added). I will now set a value for description to see the results of the query:

Articlemodel.findone ({title: ' Hello '}, Function (err, article) {    Article.remark = ' remark ';    Article.description = ' This is description ';    console.log (err, article);}); Result: {"description": "This is description", "Content": "This is a test article", "title": "Hello", "_id": "56f5ee83fcfad37f1371e952", "__v": 0, "STA Tus ": 0," create_at ":" 2016-03-26t02:05:55.814z "}

We found that the description assignment was successful, but the added remark property was still invalid.

What is this for? Because Mongoose is an ODM (object Document Mapper), similar to the ORM used by the operational relational database (object Relational Mapper), The structure of the data we use to mongoose is dependent on the schema structure we define. The added remark property is not defined in the schema, so it is not valid to add the remark property to the resulting result, and the Description property has previously been defined in the struct (not added), so the value can be reset.

Conclusion: Objects used by Mongoose in MongoDB cannot add attributes.

The question then is, what if I need to add a new attribute to the results?

Method 1, in the schema directly add the properties that need to be supplemented.

Exports.schema = new Schema ({    title:string,//title    description:string,//Description    content:string,//content        Remark:string,//Memo (Supplemental new attribute, now same as description)    create_at: {type:date, default:date.now}//add Time});

Method 2, clone the result of the query to an object, and then supplement the properties in the new object.

Articlemodel.findone ({title: ' Hello '}, Function (err, article) {var newobj = null; if (article) {    newobj = {        _id:article._id,        title:article.title,//title        Description:     article.description,//description        content:article.content,//content        remark: "Remarks",        Create_at:article.create_at, Status:article.status, status_name:article.status==1? '  Publish ': ' not published ';    }; }    Console.log (newobj);});

Method 3: Like the example above in the schema already has status representation state, if we just need a status_name to show the state of the article in Chinese interpretation. Instead of cloning new objects, you can use the schema's virtual properties.

The statement is modified as follows: var mongoose = require (' Mongoose '); var Schema = Mongoose. Schema;var schema = new schema ({    title:string,//title    description:string,//Description    content:string,//content    Status: {Type:number, defalut:0},//unpublished: 0, Release: 1    create_at: {type:date, default:date.now}//add Time}); Schema.virtual ( ' Status_name '). Get (function () {    return this.status = = 1? ' Publish ': ' Not published ';}); Exports.schema = schema; directly using the Status_name property after querying the result: Articlemodel.findone ({title: ' Hello '}, Function (err, article) {    Console.log (article.stauts_name);});

MongoDB objects taken with mongoose cannot add attributes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.