Extract object after firing rules - drools

How to pass the list of objects as a values to the main object in JSON data and get the entire object in response body.
{
"commands": [{
"insert": {
"object": {
"transaction": {
"number": "11223344",
"checklist": [{
"fLegalAmount": 500,
"fCourtesyAmount": 300
},
{
"fLegalAmount": 300,
"fCourtesyAmount": 100
}
]
}
},
"disconnected": false,
"out-identifier": "check",
"return-object": true,
"entry-point": "DEFAULT"
}
},
{
"fire-all-rules": {}
}
]
}

Related

Update element in array in mongo db

I have a document structured like this:
{
"shedule": [
{
"SUN": {
"status": false,
"task": []
}
},
{
"MON": {
"status": false,
"task": []
}
},
]
}
How to update status to false and push new element to task array in sun object.
Here is the expected output.
{
"shedule": [
{
"SUN": {
"status": true,
"task": [1,2]
}
},
{
"MON": {
"status": false,
"task": []
}
},
]
}
If your SUM always at the first place, this is the answer:
db.collection.update(
{},
{
$push: {
"shedule.0.SUN.task": {
$each: [ NumberInt(1), NumberInt(2) ] } },
$set: { "shedule.0.SUN.status": true }
}
)

Inserting an object into an array field and only return the inserted object

I have the document below, and I wanted to add an object in the matches array and return only it and not the entire document, is it possible to do this with MongoDB?
{
"_id": "25113",
"title": "Jogo de futebol",
"rounds": {
"matches": [{
"firstParticipant": {
"address": "789",
"battlesWon": 0
},
"secondParticipant": {
"address": "748569874",
"battlesWon": 0
}
},
{
"firstParticipant": {
"address": "963",
"battlesWon": 0
},
"secondParticipant": {
"address": "741",
"battlesWon": 0
}
},
{
"firstParticipant": {
"address": "258",
"battlesWon": 0
},
"secondParticipant": {
"address": "789",
"battlesWon": 0
}
}
]
}
}
My code:
const player = {
firstParticipant: {
address: '74125',
battlesWon: 0,
},
secondParticipant: {
address: '963258',
battlesWon: 0,
},
};
const result = await this.tournamentModel.findOneAndUpdate(
{
_id: '25113',
status: 'OPEN',
},
{ $addToSet: { 'rounds.matches': player } },
{ returnOriginal: false },
);
the problem is that the result returns the entire document and not just the object added to the array.

Unable to parseMongoDB to Python dataframe in multiple rows from one record document of Mongo

I have a very deep nested array structure in Mongo.
I am trying to unwind some of the data by aggregate function, however not sure what to do next to get the results in a particular format
Here is my sample json:
{
"_id": {
"$oid": "asdf016303a7f6a"
},
"layout": [
{
"id": "",
"contents": [
{
"columns": [
{
"items": [
{
"Answer": [],
"stopBlurSave": false
}
]
}
],
"displayName": "Container"
},
{
"columns": [
{
"items": [
{
"isdirty": false,
"todaysDate": "03/21/2017"
},
{
"dbSettings": {
"Name": "Name1",
"Query": "Query1"
},
"new": false,
"stopBlurSave": false
}
]
},
{
"items": [
{
"showEditPanel": false,
"isdirty": false
},
{
"islive": false,
"new": false
}
]
}
],
"new": true
},
{
"showEditPanel": false,
"displayName": "Container",
"columns": [
{
"items": [
{
"dbSettings": {
"Name": "Name2",
"Query": "Query2"
},
"new": false,
"stopBlurSave": false
}
]
}
],
"new": true
}
],
"displayName": "Section"
}
],
"name": "Task Form"
}
If you see inside layout.contents.columns.items array there may or may not be dbSettings.
I want to take the value in the dbSettings and then create a dataframe table like below
Name DBName Query
Task Form Name1 Query1
Task Form Name2 Query2
Currently i am trying with pymongo and doing the below aggregation
collection.aggregate([
{
'$match': {
'layout.contents.columns.items.dbSettings.Name': {
'$exists': True
}
}
},
{
'$unwind': {
'path': '$layout.contents.columns.items.dbSettings',
'preserveNullAndEmptyArrays': True
}
}, {
'$unwind': {
'path': '$layout.contents.columns.items.dbSettings',
'preserveNullAndEmptyArrays': True
}
},
{
'$project': {
'_id': 0,
'name': '$name',
'db': '$layout.contents.columns.items.dbSettings.Name',
'query': '$layout.contents.columns.items.dbSettings.Query'
}
}
There are two issue here, after unwind all the data is in one column but all the null brackets are still there something like [][][][],,Name1,[]Name2[], i have tried unwind options but either those come or nothing comes in the output.
Second even after i replace everything in the data frame i am still unsure how to map it to a new row as each record can have many dbSettings i want to replicate it that many times (there is a 1:1 mapping with Name and Query).
Is it something Mongo aggregation can do or we have to do some other logic in python. Any directions are welcome.

Return only matching subdocument from array of subdocuments

I'm trying to return only the subdocument that has a matching e-mail. I've done what the documentation says here, I believe. Here's what I'm trying:
function lookupReferral(email) {
return getConnection().then(db => db.collection('Referrals').findOne(
{
emails: {$elemMatch: {name: email}}
},
{
"emails.$": 1
}
));
}
Here's an example of a document (I cut down the emails array for brevity):
{
"_id": {
"$oid": "5b65979d84b8942e04f4e346"
},
"accountCode": "auth0|5b4de18d8bed60110409ded5",
"accountEmail": "abc#gmail.com",
"emails": [
{
"name": "a#ddd.dpp",
"signedUp": false,
"updated": {
"$date": "2018-08-04T12:10:05.752Z"
}
},
{
"name": "a#ddd.dpp",
"signedUp": false,
"updated": {
"$date": "2018-08-04T12:10:05.752Z"
}
}
],
"created": {
"$date": "2018-08-04T12:10:05.985Z"
},
"updated": {
"$date": "2018-08-04T12:10:05.985Z"
}
For some reason it returns null (meaning it doesn't exists I guess?), but if I remove what I specifically want, then I get the entire document returned.
You can try this
db.collection.find({
emails: {
$elemMatch: {
name: "a#ddd.dpp"
}
}
},
{
emails: {
$elemMatch: {
name: "a#ddd.dpp"
}
}
})
See the example

filter range date elasticsearch

This is how my datas look like
{
"name": "thename",
"openingTimes": {
"monday": [
{
"start": "10:00",
"end": "14:00"
},
{
"start": "19:00",
"end": "02:30"
}
]
}
}
I want to query this document saying, opened on monday between 13:00 and 14:00.
I tried this filter but it doesn't return my document:
{
"filter": {
"range": {
"openingTimes.monday.start": {
"lte": "13:00"
},
"openingTimes.monday.end": {
"gte": "14:00"
}
}
}
}
If I simply say opened on monday at 13:00, it works:
{
"filter": {
"range": {
"openingTimes.monday.start": {
"lte": "13:00"
}
}
}
}
Or even closing on monday from 14:00, works too:
{
"filter": {
"range": {
"openingTimes.monday.start": {
"gte": "14:00"
}
}
}
}
but combining both of them doens't give me anything. How can I manage to create a filter meaning opened on monday between 13:00 and 14:00 ?
EDIT
This is how I mapped the openingTime field
{
"properties": {
"monday": {
"type": "nested",
"properties": {
"start": {"type": "date","format": "hour_minute"},
"end": {"type": "date","format": "hour_minute"}
}
}
}
}
SOLUTION (#DanTuffery)
Based on #DanTuffery answer I changed my filter to his (which is working perfectly) and added the type definition of my openingTime attribute.
For the record I am using elasticsearch as my primary db through Ruby-on-Rails using the following gems:
gem 'elasticsearch-rails', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'
gem 'elasticsearch-model', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'
gem 'elasticsearch-persistence', git: 'git://github.com/elasticsearch/elasticsearch-rails.git', require: 'elasticsearch/persistence/model'
Here is how my openingTime attribute's mapping looks like:
attribute :openingTimes, Hash, mapping: {
type: :object,
properties: {
monday: {
type: :nested,
properties: {
start:{type: :date, format: 'hour_minute'},
end: {type: :date, format: 'hour_minute'}
}
},
tuesday: {
type: :nested,
properties: {
start:{type: :date, format: 'hour_minute'},
end: {type: :date, format: 'hour_minute'}
}
},
...
...
}
}
And here is how I implemented his filter:
def self.openedBetween startTime, endTime, day
self.search filter: {
nested: {
path: "openingTimes.#{day}",
filter: {
bool: {
must: [
{range: {"openingTimes.#{day}.start"=> {lte: startTime}}},
{range: {"openingTimes.#{day}.end" => {gte: endTime}}}
]
}
}
}
}
end
First create your mapping with the openingTimes object at the top level.
/PUT http://localhost:9200/demo/test/_mapping
{
"test": {
"properties": {
"openingTimes": {
"type": "object",
"properties": {
"monday": {
"type": "nested",
"properties": {
"start": {
"type": "date",
"format": "hour_minute"
},
"end": {
"type": "date",
"format": "hour_minute"
}
}
}
}
}
}
}
}
Index your document
/POST http://localhost:9200/demo/test/1
{
"name": "thename",
"openingTimes": {
"monday": [
{
"start": "10:00",
"end": "14:00"
},
{
"start": "19:00",
"end": "02:30"
}
]
}
}
With a nested filter query you can search for the document with the start and end fields within boolean range queries:
/POST http://localhost:9200/demo/test/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "openingTimes.monday",
"filter": {
"bool": {
"must": [
{
"range": {
"openingTimes.monday.start": {
"lte": "13:00"
}
}
},
{
"range": {
"openingTimes.monday.end": {
"gte": "14:00"
}
}
}
]
}
}
}
}
}
}
}