MongoDB: How to Sort a Query Before Updating

Jon Cursi

I'm writing a Meteor (Node.js) app which uses MongoDB on the backend. At a certain point in my code, I need to update a specific document within a collection. I need to use Mongo's update() method, but I'm having trouble passing in the proper (complex) query to narrow down to that one, specific document. The document I'm trying to update is:

db.collection.find({market: 'AAA'}).sort({creationDate:-1}).limit(1)

In words, the one document in collection that has a market of AAA and was created most recently (creationDate is a UNIX timestamp number, e.g. 1408829429914). There are multiple documents with a market of AAA, so I am using sort() and limit(1) to find the document which was created most recently.

Mongo's update() doesn't seem to accept sorting parameters as part of the query before the update process. What can I do to narrow down to this document and update it? Thanks!

David Weldon

You will need to fetch the document you want and then update it by _id. If your collection was called Markets, the code would look like:

var market = Markets.findOne({market: 'AAA'}, {sort: {creationDate:-1}});
Markets.update(market._id, {$set: {fancy: true}});

It's worth mentioning that even if MongoDB supported the optimization you are looking for, meteor client code can only update by _id anyway.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to sort a query result by the result of a function in mongoDB

From Dev

NodeJS + MongoDB Sort Query

From Dev

mongodb sort within the query?

From Dev

MongoDB+Doctrine: How to sort the query by text search score

From Dev

Updating a collection with a query that specifies sort and limit

From Dev

Sort and Group in one MongoDB query

From Dev

MongoDB updating nested array with sort and slice, doesn't sort desc

From Dev

How to sort by count on Mongodb

From Dev

How To Sort This Data in MongoDB?

From Dev

How to sort that query?

From Dev

How to query Mongoose with .sort()

From Dev

How to Save Current Drivers before Updating?

From Dev

How to highlight specific packages before updating

From Dev

DiffUtil - How to keep the old List before updating it

From Dev

maximum limit for MongoDB 2.6 sort query

From Dev

MongoDB Query, sort then take nth document for group

From Dev

mongodb sort and regex query in efficient way

From Dev

MongoDB: sort and limit query with 'or' operator on embedded documents

From Dev

mongodb pagination by range query with another sort field

From Dev

Sort and Group in one MongoDB aggregation query

From Dev

mongodb pagination by range query with another sort field

From Dev

MongoDB Query: How to add some days to a date field before comparing it with a given date

From Dev

MongoDB Query: How to add some days to a date field before comparing it with a given date

From Dev

How can I sort mongodb regex search query results based on regex match count

From Dev

In MongoDB, how can I sort a 2d indexed $near query when there are over 100 records?

From Dev

MongoDB-PHP: How to sort the result of find query by timestamp in ascending/descending order?

From Dev

How to sort MongoDB results with MongoEngine?

From Dev

How to sort results by month in MongoDB

From Dev

How to sort a collection by row in MongoDB?

Related Related

  1. 1

    How to sort a query result by the result of a function in mongoDB

  2. 2

    NodeJS + MongoDB Sort Query

  3. 3

    mongodb sort within the query?

  4. 4

    MongoDB+Doctrine: How to sort the query by text search score

  5. 5

    Updating a collection with a query that specifies sort and limit

  6. 6

    Sort and Group in one MongoDB query

  7. 7

    MongoDB updating nested array with sort and slice, doesn't sort desc

  8. 8

    How to sort by count on Mongodb

  9. 9

    How To Sort This Data in MongoDB?

  10. 10

    How to sort that query?

  11. 11

    How to query Mongoose with .sort()

  12. 12

    How to Save Current Drivers before Updating?

  13. 13

    How to highlight specific packages before updating

  14. 14

    DiffUtil - How to keep the old List before updating it

  15. 15

    maximum limit for MongoDB 2.6 sort query

  16. 16

    MongoDB Query, sort then take nth document for group

  17. 17

    mongodb sort and regex query in efficient way

  18. 18

    MongoDB: sort and limit query with 'or' operator on embedded documents

  19. 19

    mongodb pagination by range query with another sort field

  20. 20

    Sort and Group in one MongoDB aggregation query

  21. 21

    mongodb pagination by range query with another sort field

  22. 22

    MongoDB Query: How to add some days to a date field before comparing it with a given date

  23. 23

    MongoDB Query: How to add some days to a date field before comparing it with a given date

  24. 24

    How can I sort mongodb regex search query results based on regex match count

  25. 25

    In MongoDB, how can I sort a 2d indexed $near query when there are over 100 records?

  26. 26

    MongoDB-PHP: How to sort the result of find query by timestamp in ascending/descending order?

  27. 27

    How to sort MongoDB results with MongoEngine?

  28. 28

    How to sort results by month in MongoDB

  29. 29

    How to sort a collection by row in MongoDB?

HotTag

Archive