Mongo query to check whether subobject exist or not - mongodb

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"
}
}

Related

Spring Data mongo - issue with Distinct collection

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);
}
}

how to match two fields in different lists in mongodb for update query

I need help please, I have to match two embedded document with their ids
for example my document structure is like this:
{
"id" : "00025",
"ean" : "01213134325",
"notify_stock" : NumberLong(10),
"specs" : [
{
"id" : "0005",
"spec" : [
{
"lang" : "fr-FR",
"text" : "taille"
}
],
"value" : [
{
"lang" : "fr-FR",
"text" : "grand"
}
]
}]
}
i have a file for multi language like this
sku_id;spec_id;spec_name;spec_value
00025;0005;size;big
00025;0006;color;black
so my query in mongodb is like this:
Modifier update query:
{
"skus.id" : "<string val>",
"skus.$.specs.id" : "<string val>"
}
Modifier update details:
{
"$push" : {
"skus.$.specs.$.spec" : {
"lang" : "<string val>",
"text" : "<string val>"
},
"skus.$.specs.$.value" : {
"lang" : "<string val>",
"text" : "<string val>"
}
}
}
when i execute this in Pentaho data integration with the step output mongodb, i got an error
"Too many positional (i.e. '$')
elements found in path 'skus.$.specs.$.spec'"

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"})

How to generate mongodb object id for a sub-document on insert

Inserting a document that also has sub-documents. I am doing this through cli. How do I make mongodb generate a object id for the Alerts sub-document record? I want something like the following, but this does not work from cli.
db.user.insert(
{
"Email" : "andy+5#domain.com",
"Domain" : "other",
"Type" : "local",
"FName" : "Andy",
"Alerts" : [
{
"_id" : new ObjectId(),
"Keyword" : "sales",
"Frequency" : {
"Type" : "daily"
},
"IsActive" : true
},
{
"_id" : new ObjectId(),
"Keyword" : "customer service",
"Frequency" : {
"Type" : "daily"
},
"IsActive" : true
}
]
}
)
You can run that exact script on the console on robomongo, it would execute just fine. Don't do it on add or edit dialog.

After updating to mongodb 2.2.0 findAndModify query gives error message for '$'positional operator

The same query works fine with mongodb 2.0.6. After updating it to 2.2.0 gives an error and not performing the operation.
"errmsg" : "exception: can't append to array using string field name [$]"
This occurs at the following line:
db.findAndModify(query, null, null, false, updateJob, true, false);
where the query is:
{ "_id" : ObjectID , "job" : { "$elemMatch" : { "jobId" : "1"}}}
and updateJob is:
{ "$set" : { "job.**$**.endTime" : { "$date" : "2012-09-05T04:12:44.708Z"}}
**
My Test collection is as below and I want to update my first job and set the new endtime.
{
"_id" : "5f6761d1-589a-4140-9753-5e890ab3ecb9",
"name" : "Test",
"lastJobId" : 2,
"job" : [{
"jobId" : "1",
"jobName" : "test1",
"endTime" : ISODate("2012-09-07T17:39:43.032Z"),
}, {
"jobId" : "2",
"jobName" : "test2",
"endTime" : ISODate("2012-09-07T17:39:43.838Z"),
}]
}
and query used for this is :
{ findandmodify : "Test", query : { _id : "5f6761d1-589a-4140-9753-5e890ab3ecb9", job : { $elemMatch : { jobId : "1" } } }, update : { $set : { job.$.endTime : new Date(1347039583032) } , new : true }
Your query:
{ "_id" : ObjectID , "job" : { "$elemMatch" : { "jobId" : "1"}}}
does not require the use of "$elemMatch". $elemMatch is specifically for the case where you are trying to match several fields inside of an array element.
Since you only have one, you can eliminate $elemMatch and just use {"job.jobId":"1"} as the query filter along with "_id" and then your findAndModify will work fine.
There is a bug that prevents findAndModify with $elemMatch and positional operator update from working properly in 2.2 which is filed here.