If we want to check that the record is exists in Collection or not, then there is an operator $exists in Mongodb. But if we want to know multiple records exists in Collection then how can we check that in single query using java driver?
For Example I have two document:
{"key": "val1"}
{"key": "val2"}
Now if I want to check that 'val1' and 'val2' is exist or not then how can we do that in single query using java driver?
Note: here field name is same in both the documents.
You need to use $in operator for that
db.collection.find( { key : { $in : ['val1','val2'] } } );
equivalent java code might like this
List<string> values = new ArrayList<string>();
values.add("val1")
values.add("val2")
BasicDBObject query = new BasicDBObject();
query.put("key", new BasicDBObject("$in", values));
DBCursor cursor = yourcollection.find(query);
am not much of a java guy, this is going to be more or less same.
Related
All of the documents in my collection contain a string field, "sourceTimeStamp" which looks like for example, 2018-11-15T14:20:06. I am trying to come up with a way to get a particular day's worth of data. I can access the data directly from RoboMongo using:
db.getCollection('archive_Nov_15_8pm_2018').find({ "tfms_object.sourceTimeStamp" : { $regex : /^2018-11-25*/}})
This returns many documents. But I need to do this using JAVA so I tried this:
DBCollection collection = db.getCollection(ARCHIVE_COLLECTION);
Pattern pat = Pattern.compile("^2018-11-15.*");
BasicDBObject query = new BasicDBObject("departureTime", pat);
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
query.put("$and", obj);
However, I get 0 documents returned. Any ideas?
How to get all the document under array in mongodb java. My Database is as below. Want to retrieve all the data under array 198_168_1_134.
below is some of What i tried,
eventlist.find(new BasicDBObject("$match","192_168_10_17"))
eventlist.find(new BasicDBObject("$elemMatch","192_168_10_17"))
eventlist.find(null, new BasicDBObject("$192_168_10_17", 1))
You have two options:
using .find() with cherry-picking which document you have to have fetched.
using the aggregation framework by projecting the documents.
By using .find() , you can do:
db.collection.find({}, { 192_168_10_17 : 1 })
By using the aggregation framework, you can do:
db.collection.aggregate( { $project : { 192_168_10_17 : 1 } } )
which will fetch only the 192_168_10_17 document data.
Of course, in order to get this working in Java, you have to translate these queries to a corresponding chain of BasicDBObject instances.
By using mongo java driver you can do this by following query -
eventlist.find(new BasicDBObject(), new BasicDBObject("198_168_1_134", 1))
I am creating a document in mongodb using something like this:
DBCollection coll =<code to get collection>;
WriteResult res = coll.insert(obj, new WriteConcern());
I then want the GUID of the newly inserted thing. Doing a search for obj would work, but that would be not efficient at all.
You aren't setting the write concern correctly, it is better to use the static ones defined in the class. After an insert the document _id can be found in the object you just inserted:
DBCollection coll =<code to get collection>;
DBObject obj = new BasicDBObject("foo",42);
coll.insert(obj, WriteConcern.ACKNOWLEDGED);
System.out.println("New _id:" + obj.get("_id");
I rarely use the WriteResult returned by inserts for anything.
How would you write this MongoDB query using Java driver :
db.customers.find({'arrayName' : {$exists:true}, $where:'this.arrayName.length>0'})
Cheers,
Yann
To build a query with the Java driver, you substitute any Javascript objects with DBObject's.
DBObject condition = new BasicDBObject();
condition.put("arrayName", new BasicDBObject("$exists", true));
condition.put("$where", "this.arrayName.length>0");
DBCursor result = yourDatabase.getCollection("customers").find(condition);
I have an object that was stored via mongo-java-driver. Object uses java.util.UUID for its _id field. Following is presentation of object via mongo shell:
> db.b.find()
{ "_id" : BinData(3,"zUOYY2AE8WZqigtb/Tqztw==") }
I have a requirement to process searching via $where clause. I use following code to do it:
Mongo m = new Mongo();
DBCollection coll = m.getDB("a").getCollection("b");
coll.save(new BasicDBObject("_id", UUID.randomUUID()));
// ??? - don't know what should be specified
DBObject query = new BasicDBObject("$where", "this[\"_id\"] == " + ???);
coll.find(query).count()
The question is what should I specify instead of ??? to make it work?
Thanks for any help.
My invesigation shown that only one way to do it is rewriting a query in object based way (I mean migration of $where clause part to BasicDBObject based query). In such case mongo-java-driver supports java.util.UUID without any additional effort.