First, define a goods (commodity) of the models
varMongoose = require ('Mongoose');varSchema =Mongoose. Schema;varProductschema =NewSchema ({"productId": String,"Producname": String,"Saleprice": Number,"productimage": String}); Module.exports=mongoose.model ("Good", Productschema,'Goods');
Two, in the definition of a users (user) models
varMongoose = require ('Mongoose');varUserschema =NewMongoose. Schema ({"userId": String,"UserName": String,"userpwd": String,"orderlist": Array,"cartlist": [ { "productId": String,"Producname": String,"Saleprice": Number,"ProductName": String,"productimage": String,"checked": String,"Productnum": String}],"AddressList": Array}); Module.exports= Mongoose.model ("User", Userschema,'Users')/*COMMONJS Specification*/
The relationship between the two models can be seen: a user corresponding to a shopping cart (cartlist), a shopping cart has multiple product objects
Now let's add a product to the user (we can add it by default) ===>Userdoc for the user after login, we add products for this user's shopping cart
We goods in the route:
goods.findone ({Productid:productid}, function (ERR1, doc) { if(ERR1) {returnRes.json ({status:"1", msg:err1.message}) } Else { if(DOC) {//Product doc.productnum="1", Doc. checked="1" , UserDoc.cartList.push (DOC); Userdoc.save (function (ERR2) {if(ERR2) {returnRes.json ({status:"1", msg:err2.message}) } Else { returnRes.json ({status:"0", msg:"', Result:"suc" }) } }) } } })
After normal execution, we did not see Productnum and checked in the user's shopping cart, and the rest of the properties were assigned.
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 attributes are not defined in the (goods) schema, so it is not valid to append productnum and checked attributes to goods temporarily.
Here we need to explain that even though we are attaching attributes to the schema, it is just that the implementation can actually hang on the schema and not be added to the schema. For example, just want to implement when adding goods, by the way, the value of Productnum and checked are assigned to the users table. We do not need to store attributes in goods.
Conclusion: Objects used by Mongoose in MongoDB cannot add attributes.
Workaround One,
Add attributes that need to be added directly in the schema.
varMongoose = require ('Mongoose');varSchema =Mongoose. Schema;varProductschema =NewSchema ({"productId": String,"Producname": String,"Saleprice": Number,"productimage": String"checked": String,"Productnum": String}); Module.exports=mongoose.model ("Good", Productschema,'Goods');
This allows the two sides to be implemented in a reciprocal way, assigning values. (sometimes not very good)
workaround Two,
Clone an object to the result of the query, and then supplement the properties in the new object.
goods.findone ({Productid:productid}, function (ERR1, doc) {varnewobj =NULL;//New Objectif(ERR1) {returnRes.json ({status:"1", msg:err1.message}) } Else { if(DOC) {//Product newobj= {//Create a new object to implement the transform mongoose cannot directly increase the properties of the pit productnum: "1", checked: "1" , productId:doc.productId, Producname: Doc.producname, SalePrice:doc.salePrice, Productna Me:doc.productName, ProductImage:doc.productImage,} UserDoc.cartList.push (newobj); Userdoc.save (function (ERR2) {if(ERR2) {returnRes.json ({status:"1", msg:err2.message}) } Else { returnRes.json ({status:"0", msg:"', Result:"suc" }) } }) } } })
After execution, we can see that the procuctnum and checked of the users table in the MONGODB data are assigned values.
This is just a simple record of the learning process encountered some of the small pits.
If you have any questions, you can leave a message to discuss ........ .......
MongoDB objects obtained with Mongoose cannot add property resolution