Skip to content

Tag: mongodb-java

How to add to an existing MongoDB Bson Filter in Java

I’m using MongoDB 3.6.3 and the 3.6.0 Mongo & Bson drivers for Java. Given the following filter: I need to conditionally add another field to filter, effectively making it: Is there a way to do this by appending that field to filter, or do I have to create the filters separately? eg. Answer Filters.and() returns an instance of the private

How to read date (Timestamp) from MongoDB using Java

Am trying to read date field from MongoDB in below format In the result 2017-01-23 22:16:07.000000, instead of 000 it should be the 812 (Original value: 2017-01-23- Note: Using MongoDB Java driver 3.4. Thank you in advance! Bharathi Answer You can use Java’s SimpleDateFormat to format the date accordingly. For example, assuming you inserted the date in MongoDB using the

Mongodb avoid duplicate entries

I am newbie to mongodb. May I know how to avoid duplicate entries. In relational tables, we use primary key to avoid it. May I know how to specify it in Mongodb using java? Answer Use an index with the {unique:true} option. You can also do this across multiple fields. See this section in the docs for more details and