I have below document in Mongo DB and I wrote a java code to get data from innermost element . For some reason its not returning me any results for it.
Input data
{
"_id": "59036b0fa036cc28c8e07db6",
"srcName":"test1",
"sections": [{
"_id": "8769669696",
"data": [{
"srcKey": "Bonds",
"rowIdx": 0,
"values": [{
"srcDesc": "Assets",
"valuesNumber": 10000
},
{
"srcDesc": "NonAssets",
"valuesNumber": 75500
},
{
"srcDesc": "liabilities",
"valuesNumber": 1566
}
]
},
{
"srcKey": "01",
"rowIdx": 1,
"values": [{
"srcDesc": "NonAssets",
"valuesNumber": 1566
}]
}
]
}]
}
The result I want is
select valuesNumber from...where srcName="test1" AND srcKey="Bonds" AND srcDesc="Assets"
Java code is as below
AggregationOperation match=Aggregation.match(Criteria.where("srcName").in("test1")
.and("sections.data.values.srcDesc").in("Assets")
.and("sections.data.srcKey").in("Bonds"));
AggregationOperation unwind1=Aggregation.unwind("sections");
AggregationOperation unwind2=Aggregation.unwind("sections.data");
AggregationOperation unwind3=Aggregation.unwind("sections.data.values");
Aggregation aggregation=Aggregation.newAggregation(match,unwind1,unwind2,unwind3,match);
BasicDBObject basicDBObject=mongoTemplate.aggregate(aggregation,"InsStatData",BasicDBObject.class).getUniqueMappedResult();
It resolved my question. I just had to do a proper mapping to get Mapped results. that was not there in original code. – user3516787 just now edit
Related
I'm learning mongoDB queries and have a problem given my collection looks like:
"filename": "myfile.png",
"updatedCoordinates": [
{
"xmin": 537.640869140625,
"xmax": 1049.36376953125,
"ymin": 204.90736389160156,
"ymax": 714.813720703125,
"label": "LABEL_0",
"status": "UNCHANGED"
},
{
"xmin": 76.68355560302734,
"xmax": 544.8860473632812,
"ymin": 151.90313720703125,
"ymax": 807.1371459960938,
"label": "LABEL_0",
"status": "UNCHANGED"
}],
"predictedCoordinates": [
{
"xmin": 537.640869140625,
"xmax": 1049.36376953125,
"ymin": 204.90736389160156,
"ymax": 714.813720703125,
"status": "UNCHANGED",
"label": "LABEL_0"
}
]
and the eligible values of status are: UNCHANGED, CHANGED, UNDETECTED
How would I query: Get all the in instances from the db where status == CHANGED / UNDECTED for ANY of the values inside either updatedCoordinates or predictedCoordinates ?
It means that if status of minimum of 1 entry inside either updated or predicted is set to changed or undetected, it's eligible for my query.
I tried:
{"$or":[{"updatedCoordinates.status": "CHANGED"}, {"predictedCoordinates.status": "CHANGED"}]}
With Python dict, I can query as:
def find_eligible(single_instance:dict):
for key in ["predictedCoordinates", "updatedCoordinates"]:
for i in single_instance[key]:
if i["status"] in ["CHANGED", "UNDETECTED]: return True
return False
But retrieving 400K instances first just to filter a few ones is not a good idea.
Try running this query:
db.collection.find({
"$or": [
{
"updatedCoordinates.status": {
"$in": [
"CHANGED",
"UNDETECTED"
]
}
},
{
"predictedCoordinates.status": {
"$in": [
"CHANGED",
"UNDETECTED"
]
}
}
]
})
Mongodb playground link: https://mongoplayground.net/p/Qda-G5L1mbR
Simple use of Mongo's dot notation allows access to nested values in arrays / objects, like so:
db.collection.find({
"updatedCoordinates.status": "CHANGED"
})
Mongo Playground
I have code that uses existing collection that I want to update name field value which is present in taskMap key how i run with run liqubase changeset for that .
sample code :
"changes": [
{
"collectionName": "bulk_import_job_spec"
},
{
"findOneAndUpdate": {
{"_id": 101},
[{"$set": {"task_map": {"import": {"_id": 1, "name": "Category Association"}}}}]
},
{
"returnNewDocument": true
}
]
your questions is quite vague. Please provide some more details so that we can understand your issue.
Hello Good Developers,
I am facing a situation in MongoDB where I've JSON Data like this
[{
"id": "GLOBAL_EDUCATION",
"general_name": "GLOBAL_EDUCATION",
"display_name": "GLOBAL_EDUCATION",
"profile_section_id": 0,
"translated": [
{
"con_lang": "US-EN",
"country_code": "US",
"language_code": "EN",
"text": "What is the highest level of education you have completed?",
"hint": null
},
{
"con_lang": "US-ES",
"country_code": "US",
"language_code": "ES",
"text": "\u00bfCu\u00e1l es su nivel de educaci\u00f3n?",
"hint": null
}...
{
....
}
]
I am projecting result using the following query :
db.collection.find({
},
{
_id: 0,
id: 1,
general_name: 1,
translated: {
$elemMatch: {
con_lang: "US-EN"
}
}
})
here's a fiddle for the same: https://mongoplayground.net/p/I99ZXBfXIut
I want those records who don't match $elemMatch don't get returned at all.
In the fiddle output, you can see that the second item doesn't have translated attribute, In this case, I don't want the second Item at all to be returned.
I am using Laravel as Backend Tech, I can filter out those records using PHP, but there are lots of records returned, and I think filtering using PHP is not the best option.
You need to use $elemMatch in the first parameter
db.collection.find({
translated: {
$elemMatch: {
con_lang: "IT-EN"
}
}
})
MongoPlayground
So, I need to sort my collection on MongoDB by fields that are on an array of objects.
I have
"columns": [{
"kind": "FirstKind",
"descriptor": "Description1",
"data": "Data to be sorted"
},{
"kind": "SecondKind",
"descriptor": "Description2",
"data": "Data to be sorted"
}]
What I want to achieve is selecting "FirstKind" and "Description1" or "SecondKind" and "Description2" and sort the collection by the field data. I found a solution to do that on MongoDB, by doing:
db.getCollection('results').aggregate(
[{
"$match": {
"$and": [{
"columns.kind": "FirstKind"
}, {
"columns.descriptor": "Name"
}]
}
},{
"$sort": {
"columns.data": -1
}
},{
"$limit": 20
}]
)
My problem now is how to translate that to ReactiveMongo on Scala. I've been trying to understand this documentation: http://reactivemongo.org/releases/0.11/documentation/advanced-topics/aggregation.html but I'm really confused about it. Has anyone ever used aggregate with ReactiveMongo on Scala? Thanks!
I agree, it is not the most straightforward thing in ReactiveMongo.
I have tried to translate your code into the ReactiveMongo aggregation query syntaxt below. However I haven't run it so you might need to tweak it a little.
val dao = reactiveMongoApi.db.collection[BSONCollection]("results")
import dao.BatchCommands.AggregationFramework._
dao.aggregate(
Match(BSONDocument(
"columns.kind" -> "FirstKind",
"columns.descriptor" -> "Name")),
List(
Sort(Descending("limit")),
Limit(20)))
I have mongodb document with data as following :
[{
"name": "Robin singh",
"developer": "java",
"address": "Robin Singh,Mohali",
"search_string": ["Robin", "Singh", "java"]
}, {
"name": "Rohan singh",
"developer": "java",
"address": "Rohan Singh,Mohali",
"search_string": ["Rohan", "Singh", "java"]
}]
I want to search document with developer java with name Rohan singh and I used this query:
{"search_string":{"$all":["Robin","Singh","java"]}}
but I am getting both results.
If you have both name and developer then you can simply use find query -
db.collection.find({"name":"Rohan singh", "developer":"java"})
Additionally, If you want to check when search_string contains exact parameters which you are passing in $all then - You need to use combination of $size and $all operator to get desired result. size must be the number of parameters that you are using in $all. Here you must check size of search_string to number of params in $all so that it will search given parameters ($all) only in those search_string which size matches to number of params.
Following query might be helpful to you.
db.collection.find({
"name":"Rohan singh",
"search_string": {
$all: ["Rohan", "singh","java"]
},
"search_string": {
$size: 3 // This is the number of param you pass in $all
}
}).pretty()
Try:
db.testcollection.aggregate([
{ $match: {"search_string":{"$all":["Robin","Singh","java"]}} },
{ $group: { _id: "$name"} }
])