List<Document> docs = new ArrayList<>();
BasicDBObject query = new BasicDBObject();
query.put("publishedDateTime", "$gte:new Date(2019, 02, 25)");
for(Document doc:collection.find()) {
docs.add(doc);
}
I would like to filter out all documents where the publishedDateTime is greater than or equal to 25 Feb 2019. I do the following code as shown above.
The mongo db has documents where one of the field is
publishedDateTime: 2019-02-25 23:25:00.000
for example.
However, it returns nothing. How to get i write it correctly? thanks
You can use the new syntactic sugar provided by new mongo drivers:
import static com.mongodb.client.model.Filters.*;
List<Document> docs = new ArrayList<>();
collection.find(
gte("publishedDateTime",
LocalDate.of(2019, 02, 25)))
.into(docs);
If you want to follow the old fashion way:
List<Document> docs = new ArrayList<>();
BasicDBObject filter = new BasicDBObject();
filter.put("publishedDateTime", new Document("$gte", LocalDate.of(2019, 02, 25));
// Do not forget to send the filter to the query!
for(Document doc:collection.find(filter)) {
docs.add(doc);
}
Notice Filter.gte method is just a helper that makes same as this filter.put("publishedDateTime", new Document("$gte", new Date(2019, 02, 25)); but looks more readable.
Related
I have the following code that supposed to update or insert a Mongo document if it does not exist
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject(value);
BasicDBObject doc = new BasicDBObject(“objectData”, getBytesForObject(o))
.append(“objectKey”, key)
.append(“LastModified”, mongoDateFormat.format(new Date()));
MongoCollection<BasicDBObject> collection =
getMongoDatabase().getCollection("cache", BasicDBObject.class);
UpdateOptions updateOptions = new UpdateOptions();
updateOptions.upsert(true);
collection.updateOne(new BasicDBObject(“objectKey”, key), new BasicDBObject("$set", doc), updateOptions);
However, this code always inserts _id as 0.
How to fix this?
I need to query a mongodb db in such a way to get some kind of data hierarchy grouping by field1 and field2. Actually from shell I can run the following:
db.collection.aggregate([{'$group': {'count': {'$sum': 1}, '_id': {'field1': '$field1', 'field2': '$field2'}}}])
Does anybody already did the same with java?
It would be better if you provide sample document.
You can convert your query to Mongo-Java driver like following:
DBCollection collection = db.getCollection("collectionName");
DBObject groupFieldsInId = new BasicDBObject();
groupFieldsInId.put( "field1", "$field1");
groupFieldsInId.put( "field2", "$field2");
DBObject groupFields = new BasicDBObject( "_id", groupFieldsInId);
groupFields.put("count", new BasicDBObject( "$sum", 1));
DBObject group = new BasicDBObject("$group", groupFields );
AggregationOutput output = collection.aggregate(group);
I want to search text phase in pdf like "Labor Law". But in result, it return all file that contain the word "Labor" and "Law". please any help checking my cod below:
EnglishAnalyzer analyzer = new EnglishAnalyzer();
analyzer.setVersion(Version.LATEST);
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.parse("Labor Law");
Directory indexDirectory = FSDirectory.open(new File(indexLucencePath));
DirectoryReader dirReader = DirectoryReader.open(indexDirectory);
indexSearcher = new IndexSearcher(dirReader);
ScoreDoc[] queryResults = indexSearcher.search(query, numOfResults).scoreDocs;
List<IndexItem> results = new ArrayList<IndexItem>();
for (ScoreDoc scoreDoc : queryResults) {
Document doc = indexSearcher.doc(scoreDoc.doc);
results.add(new IndexItem(doc.get(IndexItem.TITLE), doc.get(IndexItem.CONTENT)));
}
Try
Phrase query:
Query query = parser.parse("\"Labor Law\"");
All terms must be present
Query query = parser.parse("+Labor +Law");
You can also create query yourself like this
BooleanQuery query= new BooleanQuery();
TermQuery clause1 = new TermQuery(new Term("content", "Labor"));
TermQuery clause2 = new TermQuery(new Term("content", "Law"));
query.add(new BooleanClause(clause1, BooleanClause.Occur.MUST));
query.add(new BooleanClause(clause1, BooleanClause.Occur.MUST));
There are different types of Analyzer available, please check with different Analyzer for your requirement. Comparison of Lucene Analyzers. This may also help Lucene: Multi-word phrases as search terms
i tried to remove an embedded document without succcess.
I'm looking for the java way of following instruction:
db.games.update({'_id': 73}, {$pull: {'goals': {'goal': 4}}})
The Java documentation is pretty clear, you are just constructing BSON objects to match their respective JSON counterparts as used in the shell:
BasicDBObject query = new BasicDBObject("_id", 73);
BasicDBObject fields = new BasicDBObject("goals",
new BasicDBObject( "goal", 4));
BasicDBObject update = new BasicDBObject("$pull",fields);
games.update( query, update );
Using Bson is similar.
Bson query = new Document().append("_id", 73);
Bson fields = new Document().append("goals", new Document().append( "goal", 4));
Bson update = new Document("$pull",fields);
games.updateOne( query, update );
Is there a way in which I can modify the value of one of the keys in MongoDb via its Java Driver. I tried out the following:
someCollection.update(DBObject query, DBObject update);
someCollection.findAndModify(DBObject query, DBObject update);
But both the functions completely replace the queried document with the updated document. What is the way to update only one of the value of a particular key as in the case of using $set in the mongo shell.(apart from making a completely new Document with all fields copied and one of the fields updated).
BasicDBObject carrier = new BasicDBObject();
BasicDBObject query = new BasicDBObject();
query.put("YOUR_QUERY_STRING", YOUR_QUERY_VALUE);
BasicDBObject set = new BasicDBObject("$set", carrier);
carrier.put("a", 6);
carrier.put("b", "wx1");
myColl.updateMany(query, set);
This should work, the answer which is accepted is not right above.
Try something like this:
BasicDBObject set = new BasicDBObject("$set", new BasicDBObject("age", 10));
set.append("$set", new BasicDBObject("name", "Some Name"));
someCollection.update(someSearchQuery, set);
Also look at this example.
None of the solutions mentioned above worked for me. I realized that the query should be a Document type and not a BasicDBObject :
Document set = new Document("$set", new Document("firstName","newValue"));
yourMongoCollection.updateOne(new Document("_id",objectId), set);
Where "yourMongoCollection" is of type "MongoCollection" and "objectId" of type "ObjectId"
The previous answer pointed me in the right direction, but the code to add a 2nd object to the update did not work for me. The following did:
BasicDBObject newValues = new BasicDBObject("age", 10);
newValues.append("name", "Some Name");
BasicDBObject set = new BasicDBObject("$set", newValues);
collection.update(someSearchQuery, set);
First, unless I want to reconfigure/reformat/"re-type" my values I'd go only with findAndModify and not update.
Here is a fully working example for c&p purposes... Enjoy:
Boolean updateValue(DB db, DBCollection collection, String id, String key, Object newValue)
{
DBCollection collection = db.getCollection(<collection name>);
// Identify your required document (id, key, etc...)
DBObject query = new BasicDBObject("_ID",<ID or key value>);
DBObject update = new BasicDBObject("$set", new BasicDBObject(key, newValue));
//These flags will guarantee that you'lls get the updated result
DBObject result = collection.findAndModify(query, null, null, false, update,true, true);
//Just for precaution....
if(result == null)
return false;
return result.get(key).equals(newValue);
}
According to the documents, $set is an alise for $addFields, so just use that:
var iterable = collection.aggregate(Arrays.asList(
Aggregates.addFields(new Field("foo", "bar"))
));