Sort the values by Date in mongodb

java, mongodb, mongodb-query, mysql

Solution

From the documentation you will (and you will, won't you - nod yes) read, you will find that the first argument to the find command you are using is what is called a query document. In this document you are specifying a list of fields and conditions, "comma" separated, which is the equivalent of an and condition in declarative syntax such as SQL.

The problem with your query is it was not valid, and did not match anything. The correct syntax would be as follows:

db.person.find({"source":"Naukri", dateCreated:{$exists:true}})
    .sort({dateCreated: -1})
    .limit(10)

So now this will filter by the `value` provided for "source" and where the "dateCreated" field exists, meaning it is there and it contains something.

I recommend looking at the links below, the first of the two concerned with structuring mongoDB queries and the find method and it's arguments. All of the functionality translates to every language implementation.

As for the Java API and how to use, there are different methods depending on which you are comfortable with. The API provides a BasicDBObject class which is more or less equivalent to the JSON document notation, and is sort of a hashmap concept. For something a bit more along the lines of the shell methods and a helper to be a little more like some of the dynamic languages approach, there is the QueryBuilder class which the last two links give example and information on. These allow chaining to make your query more readable.

There are many examples on Stack Overflow alone. I suggest you take a look.

http://docs.mongodb.org/manual/tutorial/query-documents/

http://docs.mongodb.org/manual/reference/method/db.collection.find/

How to do this MongoDB query using java?

http://api.mongodb.org/java/2.2/com/mongodb/QueryBuilder.html

Problem

I am new to mongodb and I am trying to sort all my rows by date. I have records from mixed sources and I trying to sort it separately. I didn't update the `dateCreated` while writing into db for some records. Later I found and I added `dateCreated` to all my records in the db. Say I have total of 4000 records, first 1000 I don't have `dateCreated`. Latest 3000 has that column. Here I am trying to get the last Updated record using `dateCreated` column. Here is my code. ``` db.person.find({"source":"Naukri"}&{dateCreated:{$exists:true}}).sort({dateCreated: 1}).limit(10) ``` This code retruns me some results (from that 1000 records) where I can't see that `dateCreated` column at all. Moreover if I change (-1) here `{dateCreated: -1}` I am getting results from some other source, but not Naukri. So I need help this cases, How do I sort by `dateCreated` to get the latest updated record and by sources also. I am using Java API to get the records from Mongo. I'd be grateful if someone helps me to find how I will use the same query with java also. Hope my question is clear. Thanks in advance.

Original source

Related problems