How to add an array to a MongoDB document using Java? - mongodb

I want to create the following document schema in mongoDB using the java driver
{
"_id": {
"$oid": "513e9820c5d0d8b93228d7e8"
},
"suitename": "testsuite_name",
"testname": "testcase_name",
"milestones": [
{
"milestone_id": "359",
"testplans": [
{
"pland_id": "965",
"runs": [
6985,
5896
]
},
{
"plan_id": "984",
"runs": [
9856,
3684
]
}
]
}
]
}
I have the following code
BasicDBObject testObject = new BasicDBObject();
BasicDBObject milestoneObject = new BasicDBObject();
testObject.put("suitename", testsuite);
testObject.put("testname", testcase);
testObject.put("milestones", new BasicDBObject("milestone_id", "2333"));
locations.insert(testObject);
But this is not generating milestone as an array. How can I add milestone as an array? I currently get this using my code
{
"_id": {
"$oid": "513f93dac5d0e2439d34308e"
},
"suitename": "test_deployment_disable_client.TestDeploymentDisableClient",
"testname": "test_deployment_disable_client",
"milestones": {
"milestone_id": "2333"
}
}

Change to something like this:
testObject.put("suitename", testsuite);
testObject.put("testname", testcase);
List<BasicDBObject> milestones = new ArrayList<>();
milestones.add(new BasicDBObject("milestone_id", "2333"));
testObject.put("milestones", milestones);
locations.insert(testObject);

You can create an ArrayList which takes in DBObjects.
List<DBObject> array = new ArrayList<DBObject>();
Add the created DBObject for the object inside the array and add it to the array object created.
array.add(/* some object */);
Finally, put the array in the main document object.
document.put("milestones", array);

Better use:
MongoClient client = new MongoClient("localhost",27017);
MongoCollection<Document> collection = client.getDatabase("db").getCollection("collection");
List<Document> docs=new ArrayList<>();
docs.add();
collection.insertMany(docs);
client.close();

Little extending to previous answer
BasicDBObject testObject = new BasicDBObject();
testObject.put("type", "milestones");
testObject.put("usecase", "milestone_type");
List<BasicDBObject> testplans = new ArrayList<>();
testplans.add(new BasicDBObject("plan_id","3232"));
testplans.add(new BasicDBObject("plan_day","sunday"));
BasicDBObject milestoneObject = new BasicDBObject();
milestoneObject.put("milestone_id", "3232");
milestoneObject.put("plans", testplans);
List<BasicDBObject> milestones = new ArrayList<>();
milestones.add( milestoneObject);
testObject.put("milestones", milestones);
locations.insert(testObject);

Related

spring boot query to get the maximum value alone from a field in an array of subdocument

{
"_id":"1",
"name":"Elon musk",
"created_by":"alien",
"versions":[
{
"version":1,
"active":true,
"group":"ALL",
},
{
"version":2,
"active":false,
"group":"ALL",
}
]
}
I need a query which returns the maximum value of versions.version which is 2
val query = Aggregation.newAggregation(
Aggregation.group("version").max("versions.version").as("maximum"),
project("maximum").and("version").previousOperation())
val groupResults = mongoTemplate.aggregate(query, test::class.java, sample::class.java)
for (results in groupResults){
println(results.maximum)
}
I tried above but it is returning only 1 but I'm expecting 2
and also is there a query which I can use in #Query
need help!!!!
this just return max version value you could customise project to get another fields
Arrays.asList(new Document("$match",
new Document("name", "Elon musk")),
new Document("$unwind",
new Document("path", "$versions")),
new Document("$sort",
new Document("versions.version", -1L)),
new Document("$limit", 1L),
new Document("$project",
new Document("version", "$versions.version")
.append("_id", 0L)))

MongoDB Java Error: A pipeline stage specification object must contain exactly one field

I am trying to get result of this MongoDB query on java.
db.fileTree.aggregate([
{
$match: {
"_id": "6062144bb25e4809548ef246",
}
},
{
$unwind: "$children"
},
{
$match: {
"children.fileName": "Test1"
}
},
{
$project: {
"_id": 0,
"fileId": "$children.fileId",
"fileName": "$children.fileName",
"directory": "$children.directory",
}
}
]).pretty()
The query works perfectly fine and it is not showing anything when there is no data. But, the query when executed from java is producing the following error:
com.mongodb.MongoCommandException: Command failed with error 40323 (Location40323): 'A pipeline stage specification object must contain exactly one field.' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "A pipeline stage specification object must contain exactly one field.", "code": 40323, "codeName": "Location40323"}
ChildFile findChildInParent(String parentId, String fileName) {
BasicDBObject idFilter = new BasicDBObject().append("_id", parentId);
BasicDBObject matchId = new BasicDBObject().append("$match", idFilter);
BasicDBObject unwindChildren = new BasicDBObject().append("$unwind", "$children");
BasicDBObject childNameFilter = new BasicDBObject().append("children.fileName", fileName);
BasicDBObject matchChildName = new BasicDBObject().append("$match", childNameFilter);
BasicDBObject projections = new BasicDBObject()
.append("_id", 0)
.append("fileId", "$children.fileId")
.append("fileName", "$children.fileName")
.append("directory", "$children.directory");
List<ChildFile> childFiles = fileCollection.aggregate(
List.of(matchId, unwindChildren, matchChildName, projections),
ChildFile.class
).into(new ArrayList<>());
return childFiles.size() > 0 ? childFiles.get(0) : null;
}
Am I missing anything here? Any help is really appreciated. Thanks 😃!
There is a typo in your code, you are missing $ for children field
Should be:
BasicDBObject unwindChildren = new BasicDBObject().append("$unwind", "$children")
Instead of:
BasicDBObject unwindChildren = new BasicDBObject().append("$unwind", "children")
Also missing $poject stage:
BasicDBObject projections = new BasicDBObject()
.append("_id", 0)
.append("fileId", "$children.fileId")
.append("fileName", "$children.fileName")
.append("directory", "$children.directory");
BasicDBObject projectionStage = new BasicDBObject().append("$project", projections);
List<ChildFile> childFiles = fileCollection.aggregate(
List.of(matchId, unwindChildren, matchChildName, projectionStage),
ChildFile.class
).into(new ArrayList<>());

How to filter documents based on an embedded array?

After reviewing this page, specifically this query
db.scores.find(
{ results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)
I used the following imports
import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.elemMatch;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
And came up with the following code to perform a similar operation (ARRAY_FIELD_NAME = "myArray")
MongoCollection<Document> collection = mongoDB.getCollection(COLLECTION_NAME);
Bson filters = and(eq("userId", userId), elemMatch(ARRAY_FIELD_NAME, eq("id", id)));
Bson projections = fields(include(ARRAY_FIELD_NAME), excludeId());
List<Document> results = (List<Document>) collection.find(filters).projection(projections).first().get(ARRAY_FIELD_NAME);
if (CollectionUtils.isEmpty(results)) {
return null;
}
if (results.size() > 1) {
throw new ApiException(String.format("Multiple results matched (User ID: %s, Array item ID: %s)", userId, id));
}
return results.get(0);
To filter documents that have the following structure
{
"_id": {
"$oid": "588899721bbabc26865f41cc"
},
"userId": 55,
"myArray": [
{
"id": "5888998e1bbabc26865f41d2",
"title": "ABC"
},
{
"id": "5888aaf41bbabc3200e252aa",
"title": "ABC"
}
]
}
But instead of getting a single or no item from the myArray field, I always get both items !
The only code that worked for me is the following
MongoCollection<Document> collection = mongoDB.getCollection(COLLECTION_NAME);
List<Bson> aggregationFlags = new ArrayList<>();
aggregationFlags.add(new Document("$unwind", "$" + ARRAY_FIELD_NAME));
aggregationFlags.add(new Document("$match", new Document("userId", userId).append(ARRAY_FIELD_NAME + ".id", id)));
aggregationFlags.add(new Document("$project", new Document("_id", 0).append(ARRAY_FIELD_NAME, "$" + ARRAY_FIELD_NAME)));
return (Document) collection.aggregate(aggregationFlags).first().get(ARRAY_FIELD_NAME);
So why does the first piece of code that should behave the same as the query shown at the beginning of the question, not filter results as expected ?
I do not need to "aggregate" results, I need to "filter" them using the user ID and array item id.
You need to use $elemMatch(projection). Something like below should work.
import static com.mongodb.client.model.Projections.elemMatch;
Bson filters = and(eq("userId", userId));
Bson projections = fields(elemMatch(ARRAY_FIELD_NAME, eq("id", id)), excludeId());

How to write multiple group by id fields in Mongodb java driver

In the below query
{ $group : {
_id : { success:'$success', responseCode:'$responseCode', label:'$label'},
max_timeStamp : { $timeStamp : 1 },
count_responseCode : { $sum : 1 },
avg_value : { $sum : "$value" },
count_success : { $sum : 1 }
}}
How _id : { success:'$success', responseCode:'$responseCode', label:'$label'}, can be translated to use in java mongodb driver.
I tried
BasicDBList list = new BasicDBList();
list.add(new BasicDBObject("success", "$success"));
list.add(new BasicDBObject("responseCode", "$responseCode"));
list.add(new BasicDBObject("label", "$label"));
AggregationOutput output = collection.aggregate(match, project, group);
and
Multi-dimension array
String [][] muitiGroupBy = {{"success", "$success"},{"responseCode", "$responseCode"},{"label", "$label"}};
etc..
But i always get like this as result
"_id" : [ { "success" : "$success"} , { "responseCode" : "$responseCode"}]
If I use only one field it works.
DBObject groupFields = new BasicDBObject( "_id", new BasicDBObject("success", "$success"));
I had a similar need and titogeo's answer from 2013 led me in the right direction after many failed attempts to translate my aggregation operation into something the Java client could handle. This is what I used:
MongoCollection<Document> myCollection = myDB.getCollection("myCollection");
Map<String, Object> multiIdMap = new HashMap<String, Object>();
multiIdMap.put("groupField1", "$groupField1");
multiIdMap.put("groupField2", "$groupField2");
Document groupFields = new Document(multiIdMap);
AggregateIterable<Document> aggregate = myCollection.aggregate(Arrays.asList(
Aggregates.group(groupFields,
Accumulators.last("lastDate", "$dateCreated"),
Accumulators.last("lastNumAvail", "$availableUnits")
)
));
I got back exactly what I needed to match the result from this:
db.myCollection.aggregate([
{"$group":{ "_id":{
groupField1: "$groupField1",
groupField2: "$groupField2"},
lastDate:
{"$last":"$dateCreated"},
lastNumAvail:
{"$last":"$availableUnits"}
}
}
]);
We did figure out how. We can achieve by using this.
Map<String, Object> dbObjIdMap = new HashMap<String, Object>();
dbObjIdMap.put("success", "$success");
dbObjIdMap.put("responseCode", "$responseCode");
dbObjIdMap.put("label", "$label");
DBObject groupFields = new BasicDBObject( "_id", new BasicDBObject(dbObjIdMap));
I could achieve this through this code (grails code and mongo-java-driver-3.2):
DBObject groupFields = new BasicDBObject()
groupFields.put('success', "\$success")
groupFields.put('responseCode', "\$responseCode")
groupFields.put('label', "\$label")
def result = collection.aggregate(Arrays.asList(Aggregates.group(groupFields, []))).iterator()

inserting a new document in the arrray of documents

I want to add a new document to the following document having an outer key "User"
{
name:himani,
User:[
{
_id:e25ffgf627627,
Name:User1
},
{
_id:fri2i2jhjh9098,
Name:User2
}
]
};
Below is my code in which I am trying to add a new document to already existing document.
My code is:
var server = MongoServer.Create("mongodb://username:password#localhost:27017/?safe=true");
SafeMode mode = new SafeMode(true);
SafeModeResult result = new SafeModeResult();
var db = server.GetDatabase("himani");
var coll = db.GetCollection("test");
BsonDocument document = new BsonDocument();
document.Add("name", "himani");
result = coll.Insert(document, mode);
BsonDocument nested = new BsonDocument();
nested.Add("1", "heena").Add("2", "divya");
BsonArray a = new BsonArray();
a.Add(2);
a.Add(5);
nested.Add("values", a);
document["3"] = new BsonArray().Add(BsonValue.Create(nested));
coll.Save(document);
var query = Query.And(
Query.EQ("name", "himani"),
Query.EQ("3.1", "heena")
);
var match = coll.FindOne(query);
var update = Update.AddToSet("3", new BsonDocument {{ "count", "2" }});
coll.Update(query, update);
I want to add a new document to the User array. I am doing this by above code but its not working.Please tell me the right way of doing it.
I don't understand your document structure at all... and the only "user" array I could find in here was a field called "3". Your code does in fact work and appends a document into the "3" array. The below is the result after running your code. Perhaps you could be more clear as to what you want your document to look like after you have "appended" a user.
{
"_id":ObjectId("4fa7d965ce48f3216c52c6c7"),
"name":"himani",
"3":[
{
"1":"heena",
"2":"divya",
"values":[ 2, 5 ]
},
{
"count":"2"
}
]
}