In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
First, a models of goods (commodity) is defined.
Var mongoose = require ('mongoose'); var Schema = mongoose.Schema;var productSchema = new Schema ({"productId": String, "producName": String, "salePrice": Number, "productImage": String}); module.exports=mongoose.model ("Good", productSchema,'goods')
Second, in defining the models of a users (user)
Var mongoose = require ('mongoose') Var userSchema = new mongoose.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 above two models can be seen: one user corresponds to a shopping cart (cartList), and a shopping cart has multiple goods objects.
Now let's add goods for the user (we can add them directly by default) = > userDoc is the user after login, and we add products for the user's shopping cart
In our goods route:
Goods.findOne ({productId: productId}, function (err1, doc) {if (err1) {return res.json ({status: "1") Msg: err1.message})} else {if (doc) {/ / goods doc.productNum= "1", doc.checked= "1", userDoc.cartList.push (doc) UserDoc.save (function (err2) {if (err2) {return res.json ({status: "1") Msg: err2.message})} else {return res.json ({status: "0", msg:'' Result: "suc"})}})})
After the normal execution of the above, we do not see productNum and checked in the user's shopping cart, and the rest of the properties are assigned.
Why is that?
Because Mongoose is an ODM (Object Document Mapper), similar to ORM (Object Relational Mapper) used to operate relational databases, the structure of the data we get using Mongoose depends on the schema structure we define. The added attributes are not defined in the (goods) schema, so it is not valid to temporarily attach productNum and checked attributes to goods.
I need to make it clear here that although we attach properties to the schema, this is only an implementation that can actually be hung on the schema, not added to the schema. For example, the above just want to add goods, by the way, assign the values of productNum and checked to the users table. We do not need to store attributes in goods.
Conclusion: objects fetched by mongoose in mongodb cannot add attributes.
Solution one
Add attributes that need to be added directly in schema.
Var mongoose = require ('mongoose'); var Schema = mongoose.Schema;var productSchema = new Schema ({"productId": String, "producName": String, "salePrice": Number, "productImage": String "checked": String, "productNum": String}); module.exports=mongoose.model ("Good", productSchema,'goods')
In this way, both sides can be equal to each other and assign values. (sometimes not very good)
Solution two
Clone the result of the query to an object, and then add properties to the new object.
Goods.findOne ({productId: productId}, function (err1, doc) {var newobj = null / / New object if (err1) {return res.json ({status: "1", msg: err1.message})} else {if (doc) {/ / Commodity newobj = {/ / create a new object Implement the conversion of productNum: "1", checked: "1", productId: doc.productId, producName: doc.producName, salePrice: doc.salePrice, productName: doc.productName, productImage: doc.productImage in mongoose } userDoc.cartList.push (newobj) UserDoc.save (function (err2) {if (err2) {return res.json ({status: "1") Msg: err2.message})} else {return res.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.
Summary
The above is the editor to introduce to you the MongoDB with Mongoose object can not increase the attribute of the perfect solution, I hope to help you, if you have any questions, please leave me a message, the editor will reply to you in time. Thank you very much for your support to the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.