Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I have a document with a field _id has type ObjectId, and a field created_at has type Date.

_id is of course increasing, and the value of created_at is current_date should be increasing.

So my question is :

  1. Is there any chance that 2 documents, A and B, A._id > B._id, but A.created_at < B.created_at.

  2. How to keep created_at as precise as possible, so the order of created_at corresponds to _id.

1 Answer

0 votes
by (106k points)

To sort by date in mongodb you can use order_by on documents collection like

In Rails

Product.order_by("created_at desc")

In Mongodb for example

db.products.find().sort({"created_at": 1}) --- 1 for asc and -1 for desc

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
2 answers
asked Sep 13, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
asked Feb 11, 2020 in Web Technology by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...