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?
Related
Using the MongoDB Java API, I have not been able to successfully locate a full example using text search. The code I am using is this:
DBCollection coll;
String searchString = "Test String";
coll.createIndex(new BasicDBObject ("blogcomments", "text"));
DBObject q = start("blogcomments").text(searchString).get();
The name of my collection that I am performing the search on is blogcomments. creatIndex() is the replacement method for the deprecated method ensureIndex(). I have seen examples for how to use the createIndex(), but not how to execute actual searches with the Java API. Is this the correct way to go about doing this?
That's not quite right. Queries that use indexes of type "text" can not specify a field name at query time. Instead, the field names to include in the index are specified at index creation time. See the documentation for examples. Your query will look like this:
DBObject q = QueryBuilder.start().text(searchString).get();
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.
I'm trying to update a Mongo objet using is _id. However i don't find the proper syntax to make it work using JavaDriver, here is what I last try.
BasicDBObject filtre = new BasicDBObject ("_id", new BasicDBObject("$oid", id_message));
then giving to the coll.update method.
I manage to make my request work from the shh but didn't manage to trasnlate it properly to Java.
(request is something like : db.message.find({"_id" : ObjectId("516a94c4e4b0a315396e4ba3")}); )
`
How do i properly traslate it to Java. (eventually using QueryBuilder)
If you're trying to translate:
db.message.find({"_id" : ObjectId("516a94c4e4b0a315396e4ba3")})
to Java, follow this basic pattern:
MongoClient mongoClient = new MongoClient();
DB db = mongoClient.getDB("testDB");
DBCollection messages= db.getCollection("message");
DBObject query = new BasicDBObject("_id", new ObjectId("516a94c4e4b0a315396e4ba3"));
DBObject messageDoc = messages.findOne(query);
The result would be stored in messageDoc.
The documentation for some reason doesn't cover this basic pattern for some reason currently.
Can you do parameterized queries with Java and MongoDB - kind of like prepared statements with JDBC?
What I'd like to do is something like this. Set up a query that takes a date range - and then call it with different ranges. I understand that DBCursor.find(...) doesn't work this way - this is kind of pseudo-code to illustrate what I'm looking for.
DBCollection dbc = ...
DBObject pQuery = (DBObject) JSON.parse("{'date' : {'$gte' : ?}, 'date' : {'$lte' : ?}}");
DBCursor aprilResults = dbc.find(pQuery, "2012-04-01", "2012-04-30");
DBCursor mayResults = dbc.find(pQuery, "2012-05-01", "2012-05-31");
...
MongoDB itself doesn't support anything like this, but then again, it doesn't take too much sense as it needs to send the query over to the server every time anyway. You can simply
construct the object in your application yourself, and just modify specific parts by updating the correct array elements.
You should use Jongo, an API over mongo-java-driver.
Here is an example with parameterized query :
collection.insert("{'date' : #}", new Date(999));
Date before = new Date(0);
Date after = new Date(1000);
Iterable<Report> results = collection.find("{'date' : {$gte : #}, 'date' : {$lte : #}}", before, after).as(Report.class);
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.