I am new to groovy scripting. I want to convert mongodb aggregate query to groovy script to query mongodb. Below is the mongodb query i want to change to groovy script:
db.VERISK_METADATA_COLLECTION.aggregate([
{ "$match":{contentType:"FRM"}},
{ "$project":{ "appCount": {$size:"$applicability"}}},
{ "$group": { "_id":null, Total: {$sum:"$appCount" } }},
{ "$project": {Total:1,"_id":0}}
])
Below is one sample record I am writing this query for:
{
"_id" : "04c64247-7030-4b0e-a2f9-da77d27b8667",
"_class" : "com.mongodb.BasicDBObject",
"documentType" : "doc",
"stateType" : "AZ",
"availabilityDate" : "",
"multiState" : "false",
"language" : "E",
"title" : "ARIZONA LIMITED EXCLUSION OF ACTS OF TERRORISM (OTHER THAN CERTIFIED ACTS OF TERRORISM); CAP ON LOSSES FROM CERTIFIED ACTS OF TERRORISM; COVERAGE FOR CERTAIN FIRE LOSSES",
"displayFormNumber" : "FP 10 53 09 05",
"mandatory" : "N",
"client_id" : "VERISK_001",
"ignored_record_type" : "new_publish_insert",
"formLobType" : "FR",
"formNumber" : "FP10530905",
"job_queue_id" : "f8ac839f-24ee-4063-bc3c-7ed00eb91f11",
"contentType" : "FRM",
"directoryName" : "FRFORMS",
"formsType" : "E",
"objectTypeCode" : "13",
"documentName" : "FP10539O",
"doc_id" : [
"f3f528e2-32ca-49f8-b287-b436befae779"
],
"update_date" : "2017-12-27 21:05:21.189 EST",
"earliestEffectiveDate_dt" : "2005-09-01T00:00:00Z",
"formStatus" : "H",
"applicability" : [
{
"filingId_dbValue_str" : "CL-2005-OFOTR",
"jurisdiction" : "AZ",
"withDrawnDate" : "2008-10-01T00:00:00Z",
"filingId" : "CL-2005-OFOTR",
"derivedFrom" : "",
"id" : "0ef65042-6b35-460e-b12a-46766c64ff27",
"circularNumber" : "LI-FR-2005-121",
"lob" : "FR",
"effectiveDate" : "2005-09-01T00:00:00Z",
"circularDate" : "2005-07-15T00:00:00Z"
}
],
"created_date" : "2017-12-27 21:05:21.106 EST",
"uri" : "VERISK_001/FRM/FP10530905"
}
Related
I am executing this query to get system log & email log data from two different collections with the following query. The two collections have different structure.
db.getCollection('system_logs').aggregate([
{$match: {entity_id: 10}},
{"$lookup": {
"from": "mail_success_log",
"localField": "entity_id",
"foreignField": "contact_id",
"as": "email_logs",
}}
])
Getting the following result:
/* 1 */
{
"_id" : ObjectId("xxxxxxxxxxxxxxxxxxx"),
"entity_id": 10,
"activity" : "yahoo - new contact added",
"created_on" : NumberLong(1614855389),
"email_logs" : [
{
"_id" : ObjectId("xxxxxxxxxxxxxxxxx"),
"created_on" : NumberLong(1614860067),
"log" : {
"subject" : "A",
"email_id" : "abc#example.com"
},
"receiver_email" : "abc#example.com",
"sender_email" : "xyz#example.com",
"contact_id": 10
},
{
"_id" : ObjectId("xxxxxxxxxxxxxxxxx"),
"created_on" : NumberLong(1614860067),
"log" : {
"subject" : "A",
"email_id" : "abc#example.com"
},
"receiver_email" : "abc#example.com",
"sender_email" : "xyz#example.com",
"contact_id": 10
},
]
}
/* 2 */
{
"_id" : ObjectId("xxxxxxxxxxxxxxxxxxx"),
"entity_id": 10,
"activity" : "yahoo - contact updated",
"created_on" : NumberLong(1614855380),
"email_logs" : [
{
"_id" : ObjectId("xxxxxxxxxxxxxxxxx"),
"created_on" : NumberLong(1614860067),
"log" : {
"subject" : "A",
"email_id" : "abc#example.com"
},
"receiver_email" : "abc#example.com",
"sender_email" : "xyz#example.com",
"contact_id": 10
},
{
"_id" : ObjectId("xxxxxxxxxxxxxxxxx"),
"created_on" : NumberLong(1614860067),
"log" : {
"subject" : "A",
"email_id" : "abc#example.com"
},
"receiver_email" : "abc#example.com",
"sender_email" : "xyz#example.com",
"contact_id": 10
},
]
}
In the above data, I am getting repeated email logs with each system log object. Is there a way to get this repeated data only once, not with every system log object ?
Note: Is this a good practice to approach this ? or I should try some other query ?
You should use $project with the lookup.
{ $project: { "<field1>": 0, "<field2>": 0, ... } } // Return all but the specified fields
https://docs.mongodb.com/manual/reference/operator/aggregation/project/
i'm working with the restaurants db in mongo
{
"_id" : ObjectId("5c66fcf59e184ea712adfba6"),
"address" : {
"building" : "97-22",
"coord" : [
-73.8601152,
40.7311739
],
"street" : "63 Road",
"zipcode" : "11374"
},
"borough" : "Queens",
"cuisine" : "Jewish/Kosher",
"grades" : [
{
"date" : ISODate("2014-11-24T00:00:00.000Z"),
"grade" : "Z",
"score" : 20
},
{
"date" : ISODate("2013-01-17T00:00:00.000Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2012-08-02T00:00:00.000Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2011-12-15T00:00:00.000Z"),
"grade" : "B",
"score" : 25
}
],
"name" : "Tov Kosher Kitchen",
"restaurant_id" : "40356068"
}
I'm tryng to filter with match in aggregate. I want to check if any score in grades is greater than 5
db.runCommand({
aggregate: "restaurants",
pipeline : [
{$match: {"grades": {$anyElementTrue: {"score": {$gt:5}}}}}
but i'm getting this error:
"errmsg" : "unknown operator: $anyElementTrue",
thanks
Try with $eleMatch
db.restaurants.aggregate([{$match: {"grades": {$elemMatch: {"score": {$gt:5}}}}}])
Hi i'm practicing mongodb and I'm stuck with a problem. I'av the following set of documents.
{
"_id" : ObjectId("57cf9a134607674792dbad9e"),
"address" : {
"building" : "351",
"coord" : [
-73.9851356,
40.7676919
],
"street" : "West 57 Street",
"zipcode" : "10019"
},
"borough" : "Manhattan",
"cuisine" : "Irish",
"grades" : [
{
"date" : ISODate("2014-09-06T00:00:00.000Z"),
"grade" : "A",
"score" : 2
},
{
"date" : ISODate("2013-07-22T00:00:00.000Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2012-07-31T00:00:00.000Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2011-12-29T00:00:00.000Z"),
"grade" : "A",
"score" : 12
}
],
"name" : "Dj Reynolds Pub And Restaurant",
"restaurant_id" : "30191841"
}
I want to fetch list of all documents where zipcode is 10019
I'm following mongodb db tutorials and i've tried the following queries but nothing seems to work and i'm getting zero errors.
db.restaurants.find({address:{zipcode:10019}});
db.restaurants.find({"address.zipcode":10019})
zipcode is a string so your query should be
db.restaurants.find({ "address.zipcode": "10019" })
instead of
db.restaurants.find({ "address.zipcode": 10019 })
i am using golang and mongodb.
my attendance collection looks like this -
{
"_id" : ObjectId("5708156b51230e8edcb01fd1"),
"college_id" : "tisl",
"stream" : "CS",
"semester" : "sem3",
"section" : "A",
"subject" : "PH301",
"date" : ISODate("2016-04-08T20:32:42.547Z"),
"teacher" : "Chandra Kanta Bhattacharya",
"atndnc" : [
{
"rollno" : "13000112115",
"name" : "Md Hossain Ahamed",
"attend" : true
},
{
"rollno" : "13000112116",
"name" : "Md Sajid Tagala",
"attend" : true
},
{
"rollno" : "13000112117",
"name" : "Nabarun Roy",
"attend" : false
},
{
"rollno" : "13000112118",
"name" : "Nikunj Mundra",
"attend" : true
}
]
}
I want to get report for each student in percentage as an array of object like:
[{"rollno" : "13000112115",
"name" : "Md Hossain Ahamed",
"prcntg" : 80},
{
"rollno" : "13000112116",
"name" : "Md Sajid Tagala",
"prcntg" : 60
},
{
"rollno" : "13000112117",
"name" : "Nabarun Roy",
"prcntg" : 90
},
{
"rollno" : "13000112118",
"name" : "Nikunj Mundra",
"prcntg" : 65
}]
and my conditions will be the following
college_id,stream,semester,section,subject,startingdate and enddate
bson.M{"$group":bson.M{"_id":{"rollno":bson.M{"$atndnc.rollno"}}}} in this line i am getting that error
This is due to the incorrect bson.M usage. You don't need to create a bson map if its a string (single value). So you could update that to:
bson.M{"$group":
bson.M{"_id": bson.M{"rollno":"$atndnc.rollno"}}
}
The equivalent of your aggregation pipeline in Go is below :
pipeline := []bson.M{
bson.M{"$match":
bson.M{"stream": "CS", "semester":"sem3", "section":"A"}},
bson.M{"$unwind": "$atndnc"},
bson.M{"$group":
bson.M{ "_id": bson.M{"rollno":"$atndnc.rollno", "name":"$atndnc.name"},
"count":bson.M{"$sum":1},
},
},
bson.M{"$project":
bson.M{"_id":"$_id.rollno", "name":"$_id.name", "count":"$count"}},
}
I would recommend to checkout MongoDB mgo driver page for examples and references.
I have the following document, I need to search for multiple items from the embedded collection"items".
Here's an example of a single SKU
db.sku.findOne()
{
"_id" : NumberLong(1192),
"description" : "Uploaded via CSV",
"items" : [
{
"_id" : NumberLong(2),
"category" : DBRef("category", NumberLong(1)),
"description" : "840 tag visual",
"name" : "840 Visual Mini Round",
"version" : NumberLong(0)
},
{
"_id" : NumberLong(7),
"category" : DBRef("category", NumberLong(2)),
"description" : "Maxi",
"name" : "Maxi",
"version" : NumberLong(0)
},
{
"_id" : NumberLong(11),
"category" : DBRef("category", NumberLong(3)),
"description" : "Button",
"name" : "Button",
"version" : NumberLong(0)
},
{
"_id" : NumberLong(16),
"category" : DBRef("category", NumberLong(4)),
"customizationFields" : [
{
"_class" : "CustomizationField",
"_id" : NumberLong(1),
"displayText" : "Custom Print 1",
"fieldName" : "customPrint1",
"listOrder" : 1,
"maxInputLength" : 12,
"required" : false,
"version" : NumberLong(0)
},
{
"_class" : "CustomizationField",
"_id" : NumberLong(2),
"displayText" : "Custom Print 2",
"fieldName" : "customPrint2",
"listOrder" : 2,
"maxInputLength" : 17,
"required" : false,
"version" : NumberLong(0)
}
],
"description" : "2 custom lines of farm print",
"name" : "Custom 2",
"version" : NumberLong(2)
},
{
"_id" : NumberLong(20),
"category" : DBRef("category", NumberLong(5)),
"description" : "Color Red",
"name" : "Red",
"version" : NumberLong(0)
}
],
"skuCode" : "NF-USDA-XC2/SM-BC-R",
"version" : 0,
"webCowOptions" : "840miniwithcust2"
}
There are repeat items.id throughout the embedded collection. Each Sku is made up of multiple items, all combinations are unique, but one item will be part of many Skus.
I'm struggling with the query structure to get what I'm looking for.
Here are a few things I have tried:
db.sku.find({'items._id':2},{'items._id':7})
That one only returns items with the id of 7
db.sku.find({items:{$all:[{_id:5}]}})
That one doesn't return anything, but it came up when looking for solutions. I found about it in the MongoDB manual
Here's an example of a expected result:
sku:{ "_id" : NumberLong(1013),
"items" : [ { "_id" : NumberLong(5) },
{ "_id" : NumberLong(7) },
{ "_id" : NumberLong(12) },
{ "_id" : NumberLong(16) },
{ "_id" :NumberLong(2) } ] },
sku:
{ "_id" : NumberLong(1014),
"items" : [ { "_id" : NumberLong(5) },
{ "_id" : NumberLong(7) },
{ "_id" : NumberLong(2) },
{ "_id" : NumberLong(16) },
{ "_id" :NumberLong(24) } ] },
sku:
{ "_id" : NumberLong(1015),
"items" : [ { "_id" : NumberLong(5) },
{ "_id" : NumberLong(7) },
{ "_id" : NumberLong(12) },
{ "_id" : NumberLong(2) },
{ "_id" :NumberLong(5) } ] }
Each Sku that comes back has both a item of id:7, and id:2, with any other items they have.
To further clarify, my purpose is to determine how many remaining combinations exist after entering the first couple of items.
Basically a customer will start specifying items, and we'll weed it down to the remaining valid combinations. So Sku.items[0].id=5 can only be combined with items[1].id=7 or items[1].id=10 …. Then items[1].id=7 can only be combined with items[2].id=20 … and so forth
The goal was to simplify my rules for purchase, and drive it all from the Sku codes. I don't know if I dug a deeper hole instead.
Thank you,
On the part of extracting the sku with item IDs 2 and 7, when I recall correctly, you have to use $elemMatch:
db.sku.find({'items' :{ '$all' :[{ '$elemMatch':{ '_id' : 2 }},{'$elemMatch': { '_id' : 7 }}]}} )
which selects all sku where there is each an item with _id 2 and 7.
You can use aggregation pipelines
db.sku.aggregate([
{"$unwind": "$sku.items"},
{"$group": {"_id": "$_id", "items": {"$addToSet":{"_id": "$items._id"}}}},
{"$match": {"items._id": {$all:[2,7]}}}
])