MongoDB month query on array records - arrays

I have one json object in mongodb which looks like this.
JSON Object :
{
"_id" : ObjectId("58736cfc7d43c305461cdba7"),
"pb_event" : [
{
"event_type" : "Birthday",
"event_date" : ISODate("2016-05-09T13:01:11.967+0000")
},
{
"event_type" : "Aniversary",
"event_date" : ISODate("2016-06-09T13:01:11.967+0000")
}
]
}
When i try below query :
db.temp.aggregate({$project : {month : {$month : "$pb_event.event_date"}}} )
It gives me error like this :
"Can't Convert from BSON type array To date"

Related

How to lookup in another collection with objectID

collection 'bookborrow'
"_id" : ObjectId("62ebbd1f4d6ab77fe3dad494"),
"userId" : "62ea90c6691228b6157cc0f9",
"data" : {
"todayDate" : "04/08/2022",
"dateofreturn" : "19/08/2022",
"bookid" : "62e53c1ff5d4c45fb7853d9e"
}
}
collection 'bookdetails'
{
"_id" : ObjectId("62e53c1ff5d4c45fb7853d9e"),
"bookname" : "How To Own Your Own Mind",
"bookauthor" : "Napoleon Hill",
"counterbooks" : "2"
}
output look like
"bookname" : "How To Own Your Own Mind",
"todayDate" : "04/08/2022",
"dateofreturn" : "19/08/2022",
bookid is uniq id .it's bookdetails match _id

mongodb updateMany nested array

I've got the following object and want to update
ArrayOne.ArrayTwo.DocumentTypeID from that ID to a different ID:
{
"_id" : NUUID("97cd06e9-3449-45db-be69-fce30ee1f9b8"),
"ArrayOne" : [
{
"ID" : NUUID("70d99c86-414c-401a-9cc9-91661806c10f"),
"ArrayTwo" : [
{
"EvidenceID" : NUUID("0799492a-b312-4832-a830-ce4ff841d5fd"),
"DocumentTypeID" : NUUID("fa67acee-2d56-4174-954f-f1bdc892add0"),
"FileID" : NUUID("e5f433c6-8c7f-008d-6d2a-12a908f91690"),
"FileName" : "testqwerty.pdf",
"AdjudicationStatus" : 0,
"SubmitDateTime" : ISODate("2022-09-12T14:14:15.060Z")
}
],
"Propertyone" : null,
"Propertytwo" : null,
"Propertythree" : 0
}
]
}
Trying to use:
db.getCollection('Collection').updateMany
(
{"ArrayOne.ArrayTwo.DocumentTypeID" : NUUID("FA67ACEE-2D56-4174-954F-F1BDC892ADD0")},
{$set : {"ArrayOne.ArrayTwo.DocumentTypeID" : NUUID("090F89EF-3AAE-45F2-853F-5B8C7B8AC0CA")}}
)
I do not want to specify the element as 'ArrayOne' can have up to 10 elements and so can "ArrayTwo" the DocumentTypeID however has a unique GUID.
Can someone please let me know how I can do this ?
Thanks

How to retrieve data from mongodb using scala

I have connected to mongodb and filter data like this:
val coll = mongoDB.getTable(db, "report")
val query = new BasicDBObject()
query.put("version",getVersion)
val fields = new BasicDBObject()
fields.put("version",1)
fields.put("project",1)
fields.put("uri",1)
fields.put("test",1)
fields.put("browser",1)
val cursor = coll.find(queryObject,fields)
then get a document like this:
{
"_id" : ObjectId("5a968c6cffac6135b09d6cd5"),
"project" : "com.sink.www.helper",
"uri" : "com.sink.www.helper.somketest",
"browser" : "firefox",
"develop" : "scala",
"duration" : "0.61",
"test" : "acceptance"
"version" : "1.0"
"try" : 1
}
{
"_id" : ObjectId("5a968daaffacc7195c2f9b6i"),
"project" : "com.sink.www.helper",
"uri" : "com.sink.www.helper.somketest",
"browser" : "firefox",
"develop" : "scala",
"duration" : "0.62",
"test" : "acceptance"
"version" : "1.0"
"try" : 1
}
{
"_id" : ObjectId("5a968ea5fface1a723ffr246"),
"project" : "com.sink.www.helper",
"uri" : "com.sink.www.helper.somketest",
"browser" : "chrome",
"develop" : "scala",
"duration" : "0.58",
"test" : "acceptance"
"version" : "1.0"
"try" : 1
}
.....
My question is when "version" is specific value like '1.0', then the field try=try + 1, I have write code like this but this will read mongodb every time, this will cost much time:
while (cursor.hasNext())
{
val details = cursor.next()
if (details.get("version").toString() == getVersion && details.get("browser").toString() == getBrowser)
{
try += 1
}
}
because there are much data in mongodb, so it's not adapt to our project. so I want to get specific data to array or others and get to judge then let try+1, I write code like this but report error:
val scenarioArray = coll.find(queryObject,fields).toArray
for (i <- 0 to scenarioArray.length - 1)
{
println(scenarioArray(i))
}
error: value length is not a member of java.util.List[com.mongodb.DBObject]
error: java.util.List[com.mongodb.DBObject] does not take parameters
I don't know how to retrieve the document to array or list or others container, could anyone get to help on this? Thanks.

Mongo Find one set of field in nested object

i have a follwing structure in mongo db
{
"_id" : ObjectId("58e8d3323fe482ef368b4567"),
"productId" : "poe10001134",
"batchdetails" : [
{
"batchidId" : "BD0166",
"batchno" : "BbfN-1444-SBRPX"
},
{
"batchidId" : "BD0167",
"batchno" : "Bff-8444-kfkff"
},
{
"batchidId" : "BD01447",
"batchno" : "Bff-8415-kfkff"
}
]
}
i want to achieve following result
"productId" : "poe10001134"
"batchno" : "Bff-8415-kfkff"
when i execute following query
db.getCollection('table').findOne( {"batchdetails.batchidId" : "BD01447"},{productId:1,"batchidId.batchno":1})
it returns all the batch no
"productId" : "poe10001134"
"batchno" : "BbfN-1444-SBRPX"
"batchno" : "Bff-8444-kfkff"
"batchno" : "Bff-8415-kfkff"
Do it like this:
db.getCollection('table').findOne( {"batchdetails.batchidId" : "BD01447"},
{productId:1,"batchidId.batchno.$":1})
The $ in the projection contains the index of the batchno array element that was matched in the query.

How to insert multivalued field in solr using lily morphline row-based

I am trying to insert a comma-seperated string as a multivalued field to my morphline configuration from a Row-based Structure in HBase.
Can any one suggest any better way or experience I am new to this.
Is there any way I can do that.
HBase-Indexer Mapper:
<?xml version="1.0"?>
<indexer table="Document_Test"
mapper="com.ngdata.hbaseindexer.morphline.MorphlineResultToSolrMapper"
unique-key-field="documentId" mapping="row">
<param name="morphlineFile" value="/path/to/morphline.conf" />
</indexer>
Morphline Conf:
{
extractHBaseCells {
mappings : [
{
inputColumn : "CF:DocumentId"
outputField : documentId
type : long
source : value
}
{
inputColumn : "CF:Persons"
outputField : persons
type : string
source : value
}
]
}
// Some command here which can be used, I tried with **java**, But didn't worked and make it a single string
}
It is just making a single string like this :
{
"persons": [
"[Panos Kammenos, King Salman, Nabil Sadek, Ehab Azmy, Hesham Abdelhamid]"
],
"documentId": 38900223,
"_version_": 1535233203724353500
}
UPDATED
Tried this one and It worked on Row-Based mappings or Tall structure.
{
extractHBaseCells {
mappings : [
{
inputColumn : "CF:DocumentId"
outputField : documentId
type : long
source : value
}
{
inputColumn : "CF:Persons"
outputField : persons
type : string
source : value
}
]
}
}
{
split{
inputField : persons
outputField : persons_multi
separator : ","
isRegex : false
}
}
You can use split command as follows:
{
extractHBaseCells {
mappings : [
{
inputColumn : "CF:DocumentId"
outputField : documentId
type : long
source : value
}
{
inputColumn : "CF:Persons"
outputField : persons
type : string
source : value
}
]
}
}
{
split{
inputField : persons
outputField : persons_multi
separator : ","
isRegex : false
}
}
Let me know if you encounter any problem.

Resources