Update document in MongoDB instead creating a new?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1252 Views - Last Post: 23 October 2019 - 08:20 AM Rate Topic: -----

#16 wantsToCode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-February 19

Re: Update document in MongoDB instead creating a new?

Posted 09 October 2019 - 10:16 PM

View Postastonecipher, on 09 October 2019 - 12:10 PM, said:

It would be, but how should it know if they are in the store or not, if it isn't checking for each?

Would it be possible to use timestamp to do this? If every document has a "created" field with current time, can I use that to skip all the documents which are older than the first one already existing in the DB?
Was This Post Helpful? 0
  • +
  • -

#17 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2957
  • View blog
  • Posts: 11,484
  • Joined: 03-December 12

Re: Update document in MongoDB instead creating a new?

Posted 10 October 2019 - 06:28 AM

You can and make that a where clause, but you may run the risk of adding something that's already there.


I wouldn't worry about performance until you actually need too. There is a thing called pre-optimization, it generally isn't something you want to do until you have identified it as a bottleneck.
Was This Post Helpful? 1
  • +
  • -

#18 wantsToCode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-February 19

Re: Update document in MongoDB instead creating a new?

Posted 23 October 2019 - 03:54 AM

Need some help with this projects API. Here's the code I've created so far to get the documents:

exports.getCars = async (req, res, next) => {
  const query = {};

  query.year = {
    $gte: req.query.year_gte,
    $lte: req.query.year_lte
  };

  if (req.query.make) query.make = req.query.make;
  if (req.query.model) query.model = req.query.model;
  if (req.query.km_lte) query.km = { $lte: req.query.km_lte };
  if (req.query.price_lte) query.price = { $lte: req.query.price_lte };
  if (req.query.currency) query.currency = req.query.currency;

  await Car.find(query, function(err, cars) {
    if (err) {
      res.send(err);
    }
    if (req.query.limit) {
      cars.length = req.query.limit;
    }
    if (cars.length < req.query.limit) {
      return cars.length;
    }
    return res.json(cars);
  });
};



I can't use the year_lte and year_gte simultaneously. Used separate they work fine but when used at the same query the query returns all the documents. Any ideas what should I do to fix this?

This post has been edited by wantsToCode: 23 October 2019 - 03:55 AM

Was This Post Helpful? 0
  • +
  • -

#19 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2957
  • View blog
  • Posts: 11,484
  • Joined: 03-December 12

Re: Update document in MongoDB instead creating a new?

Posted 23 October 2019 - 06:51 AM

Not sure why you are having issue, it creates a between range,

Car.find({ year : { $gt :  2010, $lt : 2015}});


Have you played in the mongo console for testing queries?
Was This Post Helpful? 0
  • +
  • -

#20 wantsToCode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-February 19

Re: Update document in MongoDB instead creating a new?

Posted 23 October 2019 - 08:20 AM

View Postastonecipher, on 23 October 2019 - 06:51 AM, said:

Not sure why you are having issue, it creates a between range,

Car.find({ year : { $gt :  2010, $lt : 2015}});


Have you played in the mongo console for testing queries?


In console it shows the query as it should. So it must be my code..?

Is this right?
const query = {};

  query.year = {
    $gte: req.query.year_gte,
    $lte: req.query.year_lte

  };




I also tried like this:

if (req.query.year_gte) query.year = { $gte: req.query.year_gte };
if (req.query.year_lte) query.year = { $lte: req.query.year_lte };



This was the same, they don't work when queried at the same time. This I can understand because they both set the query.year to be something.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2