Spring Data mongo - issue with Distinct collection - mongodb

Spring Data Mongo distinct doesn't works. I've following two documents.
/* 1 */
{
"_id" : ObjectId("5ca746fd92bc0733a4a6633b"),
"firstName" : "John",
"lastName" : "Kerr",
"emailId" : "john.kerr#gmail.com",
"hobbies" : [
{
"interest" : "Indoor",
"sports" : "Chess"
},
{
"interest" : "Loveoor",
"sports" : "Table Tennis"
}
],
"_class" : "com.example.Person"
}
/* 2 */
{
"_id" : ObjectId("5ca746fd92bc0733a4a6633c"),
"firstName" : "Neha",
"lastName" : "Parate",
"emailId" : "john.kerr#gmail.com",
"hobbies" : [
{
"interest" : "Indoor",
"sports" : "Chess"
},
{
"interest" : "Loveoor",
"sports" : "Table Tennis"
},
{
"interest" : "Happydoor",
"sports" : "Lawn Tennis"
}
],
"_class" : "com.example.Person"
}
When I do db.person.distinct('hobbies'), I get the distinct records easily.
[
{
"interest" : "Indoor",
"sports" : "Chess"
},
{
"interest" : "Loveoor",
"sports" : "Table Tennis"
},
{
"interest" : "Happydoor",
"sports" : "Lawn Tennis"
}
]
The same I wants to do using Spring Data Mongo or MongoTemplate. But none of the ways things are working.
#Query(value = "{}", fields = "{'hobbies' : 1}")
List<Person> findByDistinctHobbies();

In the latest version of Spring Data Mongo as per docs: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo-template.query.distinct. You can even query to the embedded document to find out the distinct documents.
Through MongoTemplate you can achieve like below:
List<Object> object = mongoTemplate.query(Person.class).distinct("hobbies").all();
for (Object object2 : object) {
Hobbies hobbies = (Hobbies) object2;
System.out.println(hobbies);
}
}

Related

Mongo query to check whether subobject exist or not

I have a collection invoice with subobject address.'invoiceId' is the key of main object(Invoice) and '130704432ST'is key for address(address subobject is saved as Map(key,value) pair).I need to check whether this subobject exists or not, if exist I need to update this. So while querying it gives no object found for below query. I am using spring Mongotemplate query in my application. So my query looks like below.
Kindly share if you find any issues with this query and also help me with Mongo query to execute in Robo 3T 1.2 for below criteria.
Query: { "_id" : { "$java" : 144068830 }, "addresses.144068830SF" : { "$exists" : true } }, Fields: { }, Sort: { }
{
"_id" : {
"invoiceId" : 130704432
},
"destStoreNbr" : 82,
"invoiceTypeCd" : "M",
"countryCode" : "US",
"invoiceNumber" : "0000000086 ",
"totalAmt" : 1038.76,
"invoiceId" : 130704432
"addresses" : {
"130704432ST" : {
"streetAddress4" : "",
"name" : "kEN-MART",
"streetAddress3" : "",
"invAddrTypeCode" : "ST",
"streetAddress2" : "",
"zipCode" : "310310",
"cityName" : "ATLANTA",
"countryCode" : "US",
"stateCode" : "AL"
}
}

Mongoose Join List

I am new to mongoose and I have two schemas as below
contentschema
{ "_id" : autogenerated
"title": "string",
"description": "string",
}
viewedschema
{ "_id" : autogenerated
"contentid": "ref content",
"viewedby": "string",
}
All the users who have viewed the content will be stored in the viewedschema collection which has the contentid reference.
Note :As the number of viewed records will be huge, i dont want to have the viewed within the content as embedded document.
In Mongoose, Is there a way to get all the contents (array of content schema) viewed. [Similar to inner join in SQL].
Thanks in Advance.
We can use $lookup to merge the documents in two different collections of same database and it performs a left outer join on the collections.
Let us the below documents in content collection
{
"_id" : ObjectId("59ef51f106b0505f997f84c8"),
"title" : "myfavoritesong",
"description" : "A wonderful composition using string instruments"
}
{
"_id" : ObjectId("59ef52ad06b0505f997f84ca"),
"title" : "myfavoritestory",
"description" : "An interesting short story with a twisted ending"
}
Documents in viewed collection
{
"_id" : ObjectId("59ef523706b0505f997f84c9"),
"contentid" : ObjectId("59ef51f106b0505f997f84c8"),
"viewedby" : "user1"
}
{
"_id" : ObjectId("59ef52f406b0505f997f84cb"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user2"
}
{
"_id" : ObjectId("59ef53c706b0505f997f84cc"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user3"
}
Final aggregate query using $lookup by combining the two collections is
db.viewed.aggregate({
$lookup:{
from : "content",
localField: "contentid",
foreignField:"_id",
as:"viewed_contents"
}
})
Result of the aggregate query for our sample data is
{
"_id" : ObjectId("59ef523706b0505f997f84c9"),
"contentid" : ObjectId("59ef51f106b0505f997f84c8"),
"viewedby" : "user1",
"viewed_contents" : [
{
"_id" : ObjectId("59ef51f106b0505f997f84c8"),
"title" : "myfavoritesong",
"description" : "A wonderful composition using string in
struments"
}
]
}
{
"_id" : ObjectId("59ef52f406b0505f997f84cb"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user2",
"viewed_contents" : [
{
"_id" : ObjectId("59ef52ad06b0505f997f84ca"),
"title" : "myfavoritestory",
"description" : "An interesting short story with a twist
ed ending"
}
]
}
{
"_id" : ObjectId("59ef53c706b0505f997f84cc"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user3",
"viewed_contents" : [
{
"_id" : ObjectId("59ef52ad06b0505f997f84ca"),
"title" : "myfavoritestory",
"description" : "An interesting short story with a twist
ed ending"
}
]
}
Please note you can also swap the collections from viewed as foreign and content as local
db.content.aggregate({
$lookup:{
from : "viewed",
localField: "_id",
foreignField:"contentid",
as:"contents_viewed_by"
}
})
Result of this aggregate query is as follows
{
"_id" : ObjectId("59ef51f106b0505f997f84c8"),
"title" : "myfavoritesong",
"description" : "A wonderful composition using string instruments",
"contents_viewed_by" : [
{
"_id" : ObjectId("59ef523706b0505f997f84c9"),
"contentid" : ObjectId("59ef51f106b0505f997f84c8"),
"viewedby" : "user1"
}
]
}
{
"_id" : ObjectId("59ef52ad06b0505f997f84ca"),
"title" : "myfavoritestory",
"description" : "An interesting short story with a twisted ending",
"contents_viewed_by" : [
{
"_id" : ObjectId("59ef52f406b0505f997f84cb"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user2"
},
{
"_id" : ObjectId("59ef53c706b0505f997f84cc"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user3"
}
]
}

MongoDB ReduceMap An Array Property that Contains Another Array

I'm still new to MongoDB and non-relational databases in general so if the answer to my question is "Dude, you're thinking about your data in the wrong way", please let me know.
That being said, here's what I'm after. I have the following data:
{
"_id" : ObjectId("549b26b370b452eeb6acecd3"),
"user" : "54530e03c575dc86d61d22f8",
"workHistory" : [
{
"description" : "",
"endDate" : null,
"name" : "My Company",
"skills" : [
{
"_id" : ObjectId("549b29c970b452eeb6acecd4")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecd5")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecda")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecdb")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecdd")
},
{
"_id" : ObjectId("549b29c970b452eeb6acece9")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecea")
},
{
"_id" : ObjectId("549b995b70b452eeb6acecf9")
},
{
"_id" : ObjectId("549b999470b452eeb6acecfa")
},
{
"_id" : ObjectId("549b9ab670b452eeb6acecfb")
}
],
"startDate" : "2013-10-01"
},
{
"description" : "",
"endDate" : "2013-10-01",
"name" : "Another Company",
"skills" : [
{
"_id" : ObjectId("549b29c970b452eeb6acecd4")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecd5")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecdb")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecdd")
},
{
"_id" : ObjectId("549b29c970b452eeb6acece1")
},
{
"_id" : ObjectId("549b29c970b452eeb6acece9")
},
{
"_id" : ObjectId("549b995b70b452eeb6acecf9")
}
],
"startDate" : "2012-04-01"
},
.....
What I am trying to do is resolve that data I can list the actual skill names under "skills" instead of just object ID's. For reference here's the skills collection:
/* 0 */
{
"_id" : ObjectId("549b29c970b452eeb6acecd4"),
"name" : "CSS",
"description" : ""
}
/* 1 */
{
"_id" : ObjectId("549b29c970b452eeb6acecd5"),
"name" : "HTML5",
"description" : ""
}
/* 2 */
{
"_id" : ObjectId("549b29c970b452eeb6acecd6"),
"name" : "Ruby",
"description" : ""
}
/* 3 */
{
"_id" : ObjectId("549b29c970b452eeb6acecd7"),
"name" : "Ruby on Rails",
"description" : ""
}
I've tried looking into using MapReduce to do some kind of join, but I can barely wrap my head around it and all the examples I'm finding don't have data this complex.
What's the best approach for getting my desired result? Side note: I'm not 100% sure that simply embedding the skills in the "skills" property is the right approach. I'm trying to keep them separate because the skills may be listed in other areas of my application separate from work history.

MongoDB find substructure

I have a mongodb with a table named Patient. When I display the content with MongoVUE I see my Patients in this format:
/* 0 */
{
"_id" : ObjectId("547c4aa9dbe9665042dddf76"),
"Patient" : {
"Maidenname" : { },
"Phone" : {
"Type" : { },
"Number" : { }
},
"Citizenship" : { },
"SSN" : 1234567,
"Profession" : { },
"systemUID" : { },
"lid" : 111,
"system" : "abc",
"Address" : {
"Street" : { },
"State" : { },
"Zip" : { },
"Country" : { },
"City" : { }
},
"Lastname" : "asdf",
"Firstname" : "Test",
"Birthdate" : 19000101,
"Identifier" : {
"id" : 123,
"system" : "abc",
"UID" : { }
}
}
}
I would like to make a find on the field Firstname with value Test, this is my query:
db.Patient.find({Firstname:"Test"})
But it returns 0 rows.
I also tried this one:
db.Patient.find({Patient : {Firstname:"Test"}})
Also 0 rows returned.
When I do a find like this:
db.Patient.find()
I get all data. (also the one with "Firstname" : "Test")
Can anyone help me with that find query?
Should try this it work well
db.patiens.find({"Patient.Firstname":"Test"})
Since Firstname is in Patient object, it is its property you need to select is as
db.Patient.find({"Patient.Firstname":"Test"})

mongodb find all records with subdocument id equals to

i have the following collection records:
> db.products.find(ObjectId("53a9a6aad901f2961403fc9b")).pretty()
{
"_id" : ObjectId("53a9a6aad901f2961403fc9b"),
"code" : "N39",
"name" : {
"en-UK" : "N39"
},
"weight" : [
90
],
"collectionId" : ObjectId("53a9a6a8d901f2961403fbe2"),
"fabric_composition" : [
{
"fabricId" : ObjectId("53a9a6a9d901f2961403fc69"),
"value" : 70
}
{
"fabricId" : ObjectId("53a9a6a9d901f2961403fc6a"),
"value" : 30
}
],
"visible" : "true",
"manufacturer" : "53a859d9d901f2e8f81ac83b"
}
and
> db.fabric.find().pretty()
{
"_id" : ObjectId("53a9a6a9d901f2961403fc69"),
"name" : [
{
"en-UK" : "Recycled Organic Cotton"
}
]
}
{
"_id" : ObjectId("53a9a6a9d901f2961403fc6a"),
"name" : [
{
"en-UK" : "Recycled Polyester"
}
]
}
how do i query the mongodb collection products to list all products that have a fabric_composition with ObjectId for Recycled Organic Cotton as an example?
any advice much appreciated
You need to use use dot notation to query subdocuments:
db.products.find({
"fabric_composition.fabricId" : ObjectId("53a9a6a9d901f2961403fc69")
});
This query will return all documents that have at least one sub-document with fabricId you're looking for.