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'}} );
Related
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/
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'
}
}
}]);
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 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 have the following document:
{
"_index" : "testdb",
"_type" : "artWork",
"_id" : "0",
"_version" : 4,
"found" : true,
"_source":{"uuid":0,
"StatusHistoryList":[
{
"ArtWorkDate":"2015-08-28T15:52:03.030+05:00",
"ArtworkStatus":"ACTIVE"
},
{
"ArtWorkDate":"2015-08-28T15:52:03.030+05:00",
"ArtworkStatus":"INACTIVE"
}
]
}
and here is the mapping of the document:
{
"testdb" : {
"mappings" : {
"artWork" : {
"properties" : {
"StatusHistoryList" : {
"type" : "nested",
"properties" : {
"ArtWorkDate" : {
"type" : "string",
"store" : true
},
"ArtworkStatus" : {
"type" : "string",
"store" : true
}
}
},
"uuid" : {
"type" : "integer",
"store" : true
}
}
}
}
}
}
Now I want to access the values of StatusHistoryList. I got null values if I do it like this:
val get = client.prepareGet("testdb", "artWork", Id.toString()).setOperationThreaded(false)
.setFields("uuid",,"StatusHistoryList.ArtworkStatus","StatusHistoryList.ArtWorkDate","_source")
.execute()
.actionGet()
var artworkStatusList= get.getField("StatusHistoryList.ArtworkStatus").getValues.toArray()
var artWorkDateList= get.getField("StatusHistoryList.ArtWorkDate").getValues.toArray()
then I got null values from the code but my document contains the values then I found this question
so after that i tried to do it like this
var smap = get.getSource.get("StatusHistoryList").asInstanceOf[Map[String,Object]]
but then a ClassCastException is thrown
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
Please help me how can I get the values of StatusHistoryList 's ArtworkStatus and ArtWorkDate values please guide me I will be very thankfull to you.
You have almost derived the solution. The GET request has retrieved the response but the problem is in parsing the response.
Let's see the problem. Below is the document that the elastic search returns as source
{
"uuid":0,
"StatusHistoryList":[
{
"ArtWorkDate":"2015-08-28T15:52:03.030+05:00",
"ArtworkStatus":"ACTIVE"
},
{
"ArtWorkDate":"2015-08-28T15:52:03.030+05:00",
"ArtworkStatus":"INACTIVE"
}
]
}
When we do get.getSource.get("StatusHistoryList") it returns List ArtWork objects and not a Map. That is the reason for the classCastException exception.
So if you cast the response to list of objects your problem will be solved.
But this would not be an ideal solution. Some of the libraries like Jackson-Faterxml does the job for you. Using the fasterxml library you can bind the json to equivalent POJO Object.