Trouble translating a MongoDB update query into spring boot - java

In my mongo database, I have the following document:
{
"_id" : ObjectId("5d1a08d2329a3c1374f176df"),
"associateID" : "1234567",
"associatePreferences" : [
{
"type" : "NOTIFICATION",
"serviceCode" : "service-code",
"eventCode" : "test-template",
"preferences" : [
"TEXT",
"EMAIL"
]
},
{
"type" : "URGENT_NOTIFICATION",
"serviceCode" : "service-code",
"eventCode" : "test-template",
"preferences" : [
"TEXT"
]
}
]
}
I am trying to add new elements to the preferences arrays based off of a given type, serviceCode, and eventCode. I was able to write this query in mongo as shown below:
db.user_communication_preferences.update(
{'associateID':'testassociate'},
{$addToSet:{'associatePreferences.$[element].preferences':"UPDATE"}},
{arrayFilters:[
{'element.serviceCode':'service-code',
'element.eventCode':'test-template',
'element.type':'NOTIFICATION'
}
]}
)
I am trying to translate this query into a java spring boot application. I saw some posts on here about doing this, but I didn't see any that incorporated arrayFilters. Can anyone lead me down the right path?

Related

How perform query that delete elements from nested array in MongoDb using Morphia?

I have the following mongoDB object
{
"_id" : ObjectId("5c3f32a4e17c5739bccb9115"),
"name" : "John",
"friends" : [
{
"name" : "Paul"
},
{
"name" : "Lisa"
}
]
}
I need to delete some element from it. In native mongodb query it looks like
db.users.update({}, {$pull: {friends: {name:"Lisa"}}})
But how can I do this via Morhia API?
I resolve this issue by using:
Query<Group> query = getDatastore().createQuery(Group.class);
UpdateOperations<Group> ops = getDatastore().createUpdateOperations(Group.class)
.removeAll("friends", new BasicDBObject("name", "Lisa"));
getDatastore().update(query, ops);

How to use aggregation on spring boot with mongodb

I am trying to get array list of specific field using aggregate function. I am trying to run below mongodb query:
db.txn.aggregate([
{ "$match" : { "name" : "piyush"}} ,
{ "$group" : { "_id" : null , "idList" : { "$push" : "$_id"}}} ,
{ "$project" : { "idList" : 1}}
])
It will return me below result:
{
"_id" : null,
"idList" : [
ObjectId("5c150672ec78951f4c1cff00"),
ObjectId("5c150673ec78951f4c1cff01"),
ObjectId("5c150673ec78951f4c1cff02")
]}
I want to implement same query using spring boot framework with mongodb so how can I achieve it?
You should try something like this :
Aggregation aggregation = Aggregation.newAggregation(
match(Criteria.where("name").is("Piyush")),
group("_id").push("_id").as("idList")
project("idList")
);
You can refer more details here :
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/

Query to find a subdocument in MongoDB

Alright, i've got a simple question. I have a simple Document in MongoDB which holds a sub-document called "penalties".
Now i want to find the Document (here with the _id "Cammeritz") by a String in the sub-document ("penalties"), e.g. "penaltyid = 0f77d885-6597-3f47-afb1-0cee2ea3ece1". Can you maybe help me? Best would be an explanation for Java but it is okay if you maybe just help with a normal MongoDB query.
{
"_id" : "Cammeritz",
"penalties" : [
{
"_id" : null,
"date" : ISODate("2017-09-25T20:01:23.582Z"),
"penaltyid" : "0f77d885-6597-3f47-afb1-0cee2ea3ece1",
"reason" : "Hacking",
"teammember" : "Luis",
"type" : "ban"
},
{
"_id" : null,
"date" : ISODate("2017-09-25T20:01:23.594Z"),
"penaltyid" : "7f5411b0-e66a-33b3-ac4f-4f3159aa88a9",
"reason" : "Spam",
"teammember" : "BluingFX",
"type" : "kick"
}
],
"isBanned" : true,
"isMuted" : false
}
Oops, I misread your question. You'll need to use dot notation. db.collection.find( { penalties.penaltyid: '0f77d885-6597-3f47-afb1-0cee2ea3ece1' } ) For more info see Query on a Nested Field.
Original answer:
db.collection.find( { penalties: "0f77d885-6597-3f47-afb1-0cee2ea3ece1" } ) should work. For more see Query an Array for an Element from the mongodb docs. I'm not very familiar with Java so I can't help much there.

How do I GroupBy in Spring Data Mongodb without Aggregation?

I have a data like below, and I want to group that data by the type, I'm using spring-data-mongodb .
[
{
"_id" : ObjectId("58a5518aace6132a88309d98"),
"type" : "SMS",
},
{
"_id" : ObjectId("58a5518bace6132a88309d99"),
"type" : "PUSH_NOTIFICATION",
},
{
"_id" : ObjectId("58a5519aace6132a0094d7df"),
"type" : "SMS",
},
{
"_id" : ObjectId("58a5519aace6132a0094d7e0"),
"type" : "PUSH_NOTIFICATION",
}
]
I'm using this method and won't work.
GroupByResults<Queuing> results = mongoTemplate.group("queuing",
GroupBy.key("type"), Queuing.class);
Anyone know the best and clear way to do this grouping using spring-data-mongodb.
Thanks.
This is the correct syntax for group operation.
GroupByResults<Queuing> results = mongoTemplate.group("queuing",
GroupBy.key("type").initialDocument("{}").reduceFunction("function(doc, prev) {}"),
Queuing.class);
More information here http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.group.example

Morphia: Filter element in ArrayList using Morphia

I have a Java class called Project with a users ArrayList<User> property that contains User objects.
My projects collection looks like
[
{
"_id" : ObjectId("56410150d277de02f3b67495"),
"name" : "my-project",
"users" : [
{
"id" : "58b5eb81-35b2-4719-87ad-e5eea76a478f",
"name" : "john",
"lastName" : "Doe"
}
]
}
]
I want to find a user with a specific UUID inside this property. I can find it in MongoDB with the following query:
db.getCollection('projects').find({"users.id": "58b5eb81-35b2-4719-87ad-e5eea76a478f"})
How do I get the same results using Morphia?
I've tried this:
Project project = datastore.createQuery(Project.class).filter("users.id", user_id).get();
And this:
Project project = datastore.find(Project.class, "users.id", user_id).get();
But with no luck :(
Any help?
I expect getting:
{
"id" : "58b5eb81-35b2-4719-87ad-e5eea76a478f",
"name" : "john",
"lastName" : "Doe"
}

Categories

Resources