Home > Web Front-end > JS Tutorial > Two ways to update objects in mongoose

Two ways to update objects in mongoose

小云云
Release: 2017-12-20 09:23:34
Original
2178 people have browsed it

Mongoose is an object model tool that performs convenient operations on mongodb in the node.js asynchronous environment. Recently, when using mongoose, I discovered a knowledge point that I had not paid attention to before. This article mainly introduces two methods about mongoose update objects. This method will be compared in detail through sample code to give everyone a better reference for learning. Friends in need can refer to it. Hope it helps everyone.

To use it, you must first install node.js and mongodb. For an introduction to the installation and operation of mongodb, please refer to:

Describe the scenario

Update the shopping cart quantity and check status

Business logic

Query the current user’s shopping cart object Cart, and update the quantity and selected fields passed by the front end

Method one

var _ = require('underscore');
Cart.findOneAndUpdate({
 _id: req.body.cart._id,
 user: user
 }, _.pick(req.body.cart, 'quantity', 'selected'), {
 new: true
 },
 function(err, updatedCart) {
 res.send(
  utils.json({
  data: updatedCart
  })
 );
 }
);
Copy after login

Note: _.pick is equivalent to

 {
 quantity: req.body.cart.quantity,
 selected: req.body.cart.selected
 }
Copy after login

Method two

var _ = require('underscore');
Cart.findOne({
 _id: req.body.cart._id,
 user: user
 }, function(err, cart) {
 if (err) {
  console.log(err);
 }
 // 复制对象
 _.extend(cart, req.body.cart);
 cart.save(function(err, updatedCart) {
  res.send(
  utils.json({
   data: updatedCart
  })
  );
 });
 }
);
Copy after login

Comparison

The first code uses findOneAndUpdate Only one step is used, which is more concise and suitable for scenarios where the updated fields are few and very clear.

The second method findsOne first and then saves the entity. It uses underscore object replication and is more flexible for the entire object operation. Suitable for scenarios with many and uncertain fields

Conclusion

Requirements are always changing, so I generally use the second option.

Related recommendations:

Detailed explanation of Mongoose's virtual field query implementation method,

Installation and use of Mongoose with Node.js operation MongoDB basic tutorial_node.js

Node.js MongoDB driver Mongoose basic usage tutorial_node.js

The above is the detailed content of Two ways to update objects in mongoose. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template