I have a structure like this:
{
"_id" : ObjectId("5a9da40e87661b3448b7dfe4"),
"userList" : [
{
"user" : {
"email" : "Arnold#mail.com",
"name" : "Arnold"
},
"key" : "ArnoldKey"
}
]
}
This query in Java works fine:
{'userList.user.email' : 'Arnold#mail.com'}
And this does not find anything:
{'userList.user.email':{ '$regex' : '.*arnold.*' , '$options' : 'i'}}
When I remove [] brackets from the structure it works fine, but It's not a solution for me. How should i query to get regex working? Any help appreciated.
You need to update your query like this:
db.getCollection('users').aggregate([{
$match: {
'userList.user.name': {
$regex: `.*arnold.*`,
$options: 'i'
}
}
}]);
Related
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);
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.
I have a mongo collection named 'demo', which is having the following structure:
{
"_id" : ObjectId("59d600182c44a11cec2b9ac5"),
"User_ID" : "user-12",
"Status" : {
"User_Status" : "Registered",
"Location" : "USA"
}
}
I have used the following mongo query to fetch data:
db.demo.find({},
{
User_ID:1,
"Status.User_Status":1
})
The output of above query is:
{
"_id" : ObjectId("59d600182c44a11cec2b9ac5"),
"User_ID" : "user-12",
"Status" : {
"User_Status" : "Registered"
}
}
But my requirement is to achieve the following output:
{
"_id" : ObjectId("59d600182c44a11cec2b9ac5"),
"User_ID" : "user-12",
"User_Status" : "Registered"
}
Is there any way to disable to parent document (Status) and get only result for sub-document "User_Status". Thanks for any help.
Yes, you can use aggregate with project instead of find
Use the below command get your desired result :
db.dummy.aggregate(
{
$project:
{
User_ID:1,
User_Status:"$Status.User_Status"
}
})
Use aggregate instead find and project operator.
db.demo.aggregate( {$project: {User_Status:'$Status.User_Status', User_ID:'$User_ID'}} );
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
I would like to apply a group operation on my entire Document using MongoDB Java Driver 3.0
My query is something like:
db.coll.group( { key: {"field": 1}, cond: {}, reduce: function(curr, result){}, initial: {} } )
Results are:
{
"field" : "A61038968K16X275KNWCEIHr"
},
{
"field" : "AH1038968716P3210C6NiQpD"
},
{
"field" : "AV1038968F16Q321DCxY7T6w"
},
{
"field" : "A71038968K165321PLiEhbGJ"
},
{
"field" : "AY1038968N16w321a537co1U"
},
{
"field" : "AJ1038968E16S3212MJpeNNV"
}
I'm trying things in Java like : collection.aggregate(group("field")) but it doesn't work. Sorry if it's easy but I can't find anything googling.
Thanks!
So, what I was looking for was a 'distinct' method.
My solution was:
collection.distinct(<string field>, <query>, <result class>);
For example, in my case, my result class is a String.
collection.distinct("field_grouping",gte("field_query", 1000),String.class);
After that you can iterate your results and do whatever you want:
Iterator<String> iterator = collection.distinct("field_grouping",gte("field_query", 1000),String.class).iterator();