I need to compare the requestParam system and Email Systems system ,if both are equal ,result value should be feteched based that system
{
"_id" : ObjectId("5f0890e870e631865877e"),
"user" : "testuser",
"Email" : "testuser#sample.com",
"Batch Systems" : [
"STAR",
"STORY",
"ITEMS",
],
"Email Systems" : [
{
"Bob" : {
"System" : "Bob",
**"result"** : true
}
},
{
"Wild" : {
"System" : "Wild",
"result" : true
}
},
{
"CRaft" : {
"System" : "Craft",
"result" : false
}
}
]
}
public Object getDetails(#RequestParam(value = "system") String userId,
MongoDatabase database = this.mongoClient.getDatabase(this.database);
MongoCollection<Document> users = database.getCollection(users);
String searchString = new String(sourceSystem);
Document matchDoc = new Document("$Email Systems.Bob.System",searchString);
Document projectDoc = new Document("$Email Systems","EmailSystem");
AggregateIterable<Document> aggregateIterable = users.aggregate(Arrays.asList( new Document("$match", matchDoc)));
Iterator iterator = aggregateIterable.iterator();
ArrayList<Document> documents = new ArrayList();
while (iterator.hasNext()) {
boolean doc = documents.add((Document) iterator.next());
About Code is giving results [Document{{Bob=Document{{System=Bob, result=true}}}}, Document{{Wild=Document{{System=Wild, result=true}}}}]
I am not able to get the Bob result value and it should be compared with the request param system .Can anyone help me the way to get the Bob result value based on above output
Related
I've got this data in mongodb
{"_id" : "pippo",
"Rich" : [
{
"_id" : "8c9379f1-ba5a-4b43-b7ad-f5fe3bf6f09d",
"Appr" : null
},
{
"_id" : "8c9379f1-265a-4b43-b7ad-f5fe3bf6f09d",
"Appr" : null
}
]},
{"_id" : "pluto",
"Rich" : [
{
"_id" : "8c9379f1-ba5a-4b43-b7ad-f5fe3bf6f09d",
"Appr" : null
},
{
"_id" : "8c9379f1-265a-4b43-b7ad-f5fe3bf6f09d",
"Appr" : null
}
]},
How can i update, with the command UpdateOne, in document "Pippo" the property "Appr" inside the array "Rich" having "_id" 8c9379f1-265a-4b43-b7ad-f5fe3bf6f09d?
thank you
Bruno
UPDATE
I try this
var filter = Builders<RichiestaGiornalieri>.Filter.Eq("id", Pippo);
var update = Builders<RichiestaGiornalieri>.Update.Set("rich.appr", new[] { new Approvazione() { Approvata = true",
Approvatore = User.Identity.Name,
DataApprovazione = DateTime.Now,
id = Guid.NewGuid().ToString(),
Note = "" }
});
var arrayFilters = new List<ArrayFilterDefinition> { new JsonArrayFilterDefinition<RichiestaGiornalieri>("{'rich.id':'8c9379f1-265a-4b43-b7ad-f5fe3bf6f09d'}") };
var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };
dbRichieste.UpdateOne(filter, update, updateOptions);
But i receive this error messagge
MongoBulkWriteException`1: A bulk write operation resulted in one or more errors.
The array filter for identifier 'rich' was not used in the update { $set: { rich.appr: [ { DataApprovazione: new Date(1599579246919), _id: "2a5cc55e-70e8-4138-ace2-6b5131cd4a9a", Approvata: true, Approvatore: "Pippo", Note: "" } ] } }
I am trying to get the array of Oject value but it is giving empty value .I need get the result
Value from the below document .
Email Systems-->result
{
"_id" : ObjectId("5f0890e870e631865877e"),
"user" : "testuser",
"Email" : "testuser#sample.com",
"Batch Systems" : [
"STAR",
"STORY",
"ITEMS",
],
"Email Systems" : [
{
"System1" : {
"System" : "Backend",
**"result"** : true
}
},
{
"System2" : {
"System" : "Backend",
"result" : true
}
}
],
Below is snippet of code :
#RequestMapping(value = "/search", method = RequestMethod.GET)
public Object getClaimsBySearchCriteria(#RequestParam(value = "userId") String userId){
MongoDatabase database = this.mongoClient.getDatabase(test);
MongoCollection<Document> users = database.getCollection(Systems);
List<? extends Bson> pipelinesVal = Arrays.asList(
new Document()
.append("$unwind", "$Email Systems"),
new Document()
.append("$match", new Document().append("Email Systems","$Email Systems")),
new Document()
.append("$project", new Document()
.append("_id", 1)
.append("items", 1)
));
List<Document> Val = new ArrayList<>();
List values=users.aggregate(pipelinesVal).into(Val);
Document docSourceSystem =new Document();
List<Document> aggregate = new ArrayList<>();
LOG.info("pipelinesVal: " + pipelinesVal.toString());
LOG.info("values: " + values.toString());
}
With the above code result value is coming as empty instead of true or false .Can anybody help me with the issue .what is going wrong with the above code
Need help on MongoDB nested query. Below is my mongo collection.
Preference collection
{
"_id" : "user123",
"preferences" : {
"product-1" : {
"frequency" : "Weekly",
"details" : {
"email" : {
"value" : "On"
}
}
},
"product-2" : {
"preferencesFor" : "mpc-other",
"preferencesForType" : "Product",
"details" : {
"email" : {
"value" : "Off"
}
}
},
"product-3" : {
"preferencesFor" : "mpc-other",
"preferencesForType" : "Product",
"details" : {
"email" : {
"value" : "On"
}
}
}
}
}
Product Collection
{
"_id" : "product-1",
"name" : "Geo-Magazine"
}
{
"_id" : "product-2",
"name" : "History-Magazine"
}
{
"_id" : "product-3",
"name" : "Science-Magazine"
}
product-1, product-2... are keys from a Map.
The keys are stored in another collection Product Collection.
Can I create a nested query to cross-reference the product keys from another table?
I need the output in the below table format. Please suggest.
user123 product-1 email On
user123 product-2 email Off
user123 product-3 email On
I tried the below but can't get result. Please suggest.
var cursor = db.productSummary.find();
while(cursor.hasNext()){
var sku = cursor.next()._id;
var skuCol = "preferences."+sku+".details.email";
var skuVal = "preferences."+sku+".details.email.value";
db.marketingPreferences.find( {}, {_id:1, skuCol:1, skuVal:1});
}
> var myCursor = db.productSummary.find();
> while(myCursor.hasNext()){
var sku = myCursor.next()._id;
var skuCol = "preferences."+sku+".details.email";
var skuVal = "$preferences."+sku+".details.email.value";
var result = db.marketingPreferences.aggregate([{"$project":{"_id":1,value:skuVal,preferences:{$literal: sku}}}],{allowDiskUse: true});
while(result.hasNext()){
printjson(result.next());
}
}
Result
{ "_id" : "user123", "preferences" : "product-1", "value" : "On" }
{ "_id" : "user123", "preferences" : "product-2", "value" : "Off" }
{ "_id" : "user123", "preferences" : "product-3", "value" : "On" }
There's a difference between MongoDB and normal SQL DB. Firstly, when you query a MongoDB collection, it doesn't return a row as it will in a SQL db. What you get here is a document similar to JSON.
Also when you use preferences.product-1.details.email : 1 it wont return you the word 'email', rather it will return you the value ie. {"value" : "On" }.
Using this: db.preference.find({},{"_id":1,"preferences.product1.details.email.value":1})
you will be able to get two details which are user123 and On and you can get product-1 from your previous query. You can store these values in a variable and keep printing them to obtain the table necessary. Also you would need another cursor to store the result of the second second query that you would do.
Here's what your query will produce if it was single standalone query:
> db.preference.find({},{"_id":1,"preferences.product1.details.email.value":1})
.pretty()
{
"_id": "user123",
"preferences": {
"product-1": {
"details": {
"email": {
"value": "On"
}
}
}
}
}
public static void test(){
MongoCollection<Document> collection = getDatadase().getCollection("product");
MongoCollection<Document> pref = getDatadase().getCollection("pref");
List<Document> allDocList = collection.find().into(new ArrayList<Document>());
for(Document doc:allDocList){
System.out.println(doc.get("_id"));
String preferences = doc.get("_id")+"";
String sku = "$preferences."+preferences+".details.email.value";
Document aggregation = new Document().append("$project", new Document().append("_id", 1).append("value", sku));
List<Document> pipeline = new ArrayList<Document>();
pipeline.add(aggregation);
List<Document> aggList = pref.aggregate(pipeline).into(new ArrayList<Document>());
for(Document doc1:aggList){
System.out.println(doc1.append("preferences", preferences));
}
}
}
This Will return
product-1
Document{{_id=user123, value=On, preferences=product-1}}
product-2
Document{{_id=user123, value=Off, preferences=product-2}}
product-3
Document{{_id=user123, value=On, preferences=product-3}}
{
"serverUsed" : "localhost/127.0.0.1:27017",
"result" : [{
"_id" : {
"$oid" : "529f131430044109e30fc6f9"
},
"html" : {
"table" : {
"tbody" : {
"Barge" : {
"Name" : "ANTVERPIA 56",
"Bargeno" : 6003696,
"Harbour" : "HH",
"Reportedpresent" : " ",
"Starting" : "06-12-2013 spil 2"
}
}
}
}
}]
}
I have this as result, how can i get string value of Name. In this case ANTVERPIA 56.
I have tried with this following code but it does not working, please help.
for (DBObject result1: output.results()){
String name1 = (String)result1.get("html.table.tbody.Barge.Name");
System.out.println(name1);
}
You cannot access nested objects by using "." in Java driver. You have to get DBObject for each nested json object. Following code should solve problem.
for (DBObject result : output.results()) {
DBObject htmlObj = (DBObject) result.get("html");
DBObject tableObj = (DBObject) htmlObj.get("table");
DBObject tbodyObj = (DBObject) tableObj.get("tbody");
DBObject bargeObj = (DBObject) tbodyObj.get("Barge");
String name = (String) bargeObj.get("Name");
}
I want to query all the UserGroup's ID where admins's ID="25160228446835585906563830293" or users's ID ="25160228446835585906563830293".
this is a hashmap key and value pair in java obj hashmap<String,Date>
"25160228446835585906563830293" : ISODate("2013-03-26T04:51:36.731Z")
{ "_id" : ObjectId("51512958849ca4748271c640"),
"_class" : "com.pcd.app.model.UserGroup",
"groupName" : "sdfsadfsad",
"privacyType" : "PRIVACY_OPEN",
"approvalType" : "MEMBER_APPROVAL",
"groupDescription" : "test",
"admins" : {
"25160228446835585906563830293" : ISODate("2013-03-26T04:51:36.731Z"),
"25160228446835585906563830294" : ISODate("2013-03-26T04:51:36.731Z"),
"25160228446835585906563830295" : ISODate("2013-03-26T04:51:36.731Z")
},
"users" : {
"25160228446835585906563830296" : ISODate("2013-03-26T04:51:36.731Z")
}
}
I'd suggest you restructure your document to make it indexable and more easily searched in MongoDB.
Instead of using the id of the admin as a field, add each admin as an object of an array:
"admins" : [
{ id: "25160228446835585906563830293",
date: ISODate("2013-03-26T04:51:36.731Z") }
],
This will make searches more natural:
db.so.find( { "admins.id" :
{ $in: ['25160228446835585906563830293',
'25160228446835585906563830296']}})
You can use the $in (docs) operator to look for admins with an id that matches the list as you wanted (admins.id).
So, given a Java QueryBuilder, it might look something like this:
BasicDBList adminIds = new BasicDBList();
adminIds.addAll(ids); // the ids could be a List<String>
DBObject inClause = new BasicDBObject("$in", adminIds);
DBObject query = new BasicDBObject("admins.id", inClause);
You may want to use ensureIndex to build an index (docs).
Based on your original example, here's the full document for reference:
{
"_id" : ObjectId("51512958849ca4748271c640"),
"_class" : "com.pcd.app.model.UserGroup",
"groupName" : "sdfsadfsad",
"privacyType" : "PRIVACY_OPEN",
"approvalType" : "MEMBER_APPROVAL",
"groupDescription" : "test",
"admins" : [
{ id: "25160228446835585906563830293" ,
date: ISODate("2013-03-26T04:51:36.731Z") },
{ id: "25160228446835585906563830294" ,
date: ISODate("2013-03-26T04:51:36.731Z") },
{ id: "25160228446835585906563830295" ,
date: ISODate("2013-03-26T04:51:36.731Z") }
],
"users" : [
{ id: "25160228446835585906563830296",
date : ISODate("2013-03-26T04:51:36.731Z") }
]
}
If you are using mongodb java driver you can do the following:
BasicDBObject queryForAdminsID = new BasicDBObject("admins." + adminsID, new BasicDBObject("$exists", true));
// BasicDBObject queryForUsersID = new BasicDBObject("users." + usersID, new BasicDBObject("$exists", true));
cursor = coll.find(query); // coll is a DBCollection
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
where usersID and adminsID are your ids