Query data inside embedded array in MongoDB - mongodb

I've an data in a collection as below.
{
"date":"01-01-2014",
"details": [
{"name":"abc",
"address":{
"city":"abc" , "state":"abc"
},
{"name":"xyz",
"address":{
"city":"xyz" , "state":"xyz"
}
}
]
}
I want all records having city as "abc" . I am using Java.
I tried below queries and the output is null.
Query searchQuery = new Query();
searchQuery.addCriteria(Criteria.where("details.address.$.city").is("abc"));
and
searchQuery.addCriteria(Criteria.where("details.address.city").is("abc"));
Any help greatly appreciated!!

Related

Mongodb query starts with inside $in Query

I want to write an $in query if column path starts with "/v1/user/1" or "/v1/user/2" then I want to select that particular document.
The sample document is as below:
{
"_id" : ObjectId("5f8872b486aa9e79d71d9809"),
"method" : "get",
"path" : "/v1/user/1/details/"
}
I tried to create a query but getting syntax error because of "/"
db.user.find({ "path": { $in: [ /^/v1/user/1/ , /^/v1/user/2/ ] } })
Could some one please help how to write the query.
Your query was almost correct. You just missed to escape the /. Try below query.
db.user.find({ "path": { $in: [ /^\/v1\/user\/1/ , /^\/v1\/user\/2/ ] } })

MongoDB: querying on a field in an array of embedded documents is not working

My document in MongoDB is like this:
{
"data":{
"groupName":"sample",
"users": [
{
"_id":new ObjectId(),
"mobNum":"29857903289",
"isAdmin":"true"
},
{
"_id":new ObjectId(),
"mobNum":"87532480923",
"isAdmin":"false"
},
]
}
}
users is an array of embedded-ducuments in groupusers collection. When I try to query the embedded sub field like this
db.groupusers.find( { "users.$.mobNum": "29857903289" } ), it returns me no response.
Problem is in querying the embedded sub field. I'm new to use MongoDB. Don't know how to query the embedded array of sub field. Not sure what to do. Any help is appreciated.
Here use this
db.getCollection('YOUR_COLLECTION_HERE').find({
"data.users": {
"$elemMatch": {
"mobNum": "29857903289"
}
}
}})

Get data from list of Sub documents in Mongo collection using Spring JPA

Below is the sample of my Mongo-collection data-structure
{
"id": "5d91fe25da1917111182ce5a",
"customName": "Chess Application",
"status":"not_ready",
"environments": [
{
"environmentId": "6bbbbda6-b01a-4b9e-99d5-a1d0f696449a",
"environmentName": "Dev",
"environmentType": "dev",
},
{
"environmentId": "3b958d27-8fb7-4edd-bbb0-1dd86437d313",
"environmentName": "qa",
"environmentType": "qa",
}
]
}
Am using spring-JPA to get the data.. I will get only the environmentId as input and i will scan all the collections and get the collection that has this environmentId
Note: the Environment-id here is not mongo-created ID. It is the UUID generated by my Java app during insertion
i used findByEnvironmentsIsIn() method and it is not helpful . Any idea on how to get only one object from the list-of-sub-documents ?
#Query("{'environments' : { $elemMatch: { 'environmentId': { $in: ?0 }}}}")
List<Object> findByEnvironmentsIsIn( Set<String> environmentIds);
I guess this should work for you

Mongo: select only one field from the nested object

In mongo I store object that have field "titleComposite". This field contains array of title object, like this:
"titleComposite": [
"0": {
"titleType": "01",
"titleText": "Test cover uploading"
}
]
I'm perfoming query and I would like to receive only "titleText" value for the returned values. Here is an example of my query:
db.onix_feed.find({"addedBy":201, "mediaFileComposite":{$exists:false}}, {"isbn13":1,"titleComposite.titleText":1})
In the results I see values like
{
"_id" : ObjectId("559ab286fa4634f309826385"),
"titleComposite" : [ { "titleText" : "The Nonprofit World" } ],
"isbn13" : "9781565495296"
}
Is there any way to get rid of "titleComposite" wrapper object and receive only titleText? For example, take titleText of the first element only?
Would appreciate any help
You can mongodb aggregation to achieve your expected result. Re-arrange your query as following...
db.onix_feed.aggregate([
{
$match: {
$and: [
{"addedBy":201},
{"mediaFileComposite":{$exists:false}}
]
}
},
{
$project : { titleText: "$titleComposite.titleText",
"isbn13" : 1 }
}
])

How can i query on embedded document using Spring data mongodb?

My document structure looks like
{
"users":[
{
"email":"user#company.com"
"messages":[
{
"id":"1",
"text":"A",
"from":"Jessy",
"isDeleted":"false"
},{
"id":"2",
"text":"B",
"from":"Jessy",
"isDeleted":"false"
},{
"id":"3",
"text":"C",
"from":"Alan",
"isDeleted":"false"
},{
"id":"4",
"text":"D",
"from":"Amy",
"isDeleted":"true"
},{
"id":"5",
"text":"E",
"from":"Amy",
"isDeleted":"false"
}
]
}
]
}
I want to query on sub document using or & and operations.So my query can be as follow.
select all those messages from the users collection having from as "Amy" and text is "E" or some other Criteria.
I have an idea about the Aggregation Framework supported by Spring data
AggregationOperation match =Aggregation.match(Criteria.where("from").is("Amy").and("text").is("E"));
AggregationOperation unwind = Aggregation.unwind("messages");
Aggregation aggregation = Aggregation.newAggregation(match);
AggregationResults<User> result = this.mongoTemplate.aggregate(aggregation, "users", User.class);
But my problem is that i want to dynamically populate the Criteria like Criteria is at clients end he or she can pass any query to the database i gonna parse that query into Criteria. Any help will be appreciated.