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 );
Related
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.
I'm trying to add a document in another document.
I am trying to insert a new document with a timestamp as a key and light prox and temp as the content of that document into document sensor_collection.
It's logical that my code doesn't work, because I'm setting a new sensor_collection. Does anyone know how I can set a timestamp document in sensor_collection or is it adviced not to do it this way?
This is the code:
MongoCollection<Document> collection = db.getCollection(Sensor.KEY_COLLECTION);
//append sensor data to existing document
collection.updateOne(doc, new Document("$set",
new Document("sensor_collection", new Document(
String.valueOf(stamp.getCurrentTime()), new Document(
Sensor.KEY_LIGHT, sensorData.getLight())
.append(Sensor.KEY_PROX, sensorData.getProx())
.append(Sensor.KEY_TEMP, sensorData.getTemp())
))));
Currently this code overrides the timestamp that's already in the db.
If you want to append to an existing embedded collection, use $push instead of $set. The $push operator appends a specified value to an array. Something like this:
collection.updateOne(doc, new Document("$push",
new Document("sensor_collection", new Document(
String.valueOf(stamp.getCurrentTime()), new Document(
Sensor.KEY_LIGHT, sensorData.getLight())
.append(Sensor.KEY_PROX, sensorData.getProx())
.append(Sensor.KEY_TEMP, sensorData.getTemp())
))));
For more details on mongo's update operators, check this out
In the Mongodb documentation I found this:
"To specify a <field> in an embedded document or in an array, use dot notation."
I used the $set operator. And I'm setting sensor_collection.timestamp
MongoCollection<Document> collection = db.getCollection(Sensor.KEY_COLLECTION);
//append sensor data to existing document
collection.updateOne(doc, new Document("$set",
new Document("sensor_collection."+String.valueOf(stamp.getCurrentTime()),
new Document(
Sensor.KEY_LIGHT, sensorData.getLight())
.append(Sensor.KEY_PROX, sensorData.getProx())
.append(Sensor.KEY_TEMP, sensorData.getTemp())
)));
This works. Gives:
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 am trying to remove duplicates in a collection using java driver in mongodb.
I am using the code
table = db.getCollection("dummy_data_OLD");
BasicDBObject query = new BasicDBObject("url", 1)
.append("unique", true).append("dropDups", true);
table.createIndex(query);
It will create a unique index , but still duplicates present in the db.
Is there any problem in my code?
This creates an index on the fields url, unique and dropDups. When you want to create an index using options, you need to provide these as a second DBObject.
DBObject fields = new BasicDBObject("url", 1);
DBObject options = new BasicDBObject("unique", true).append("dropDups", true);
db.getCollection("dummy_data_OLD").createIndex(fields, options);
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"))
));