How to Update nested Array in RethinkDB using ReQL - arrays

I have a question on Updating the array in RethinkDB. My JSON structure looks like below.
{
"LOG_EVENT": {
"ATTRIBUTES": [
{
"ATTRIBUTE1": "TYPE",
"VALUE": "ORDER"
},
{
"ATTRIBUTE2": "NUMBER",
"VALUE": "1234567"
}
],
"EVENT_CODE": [
{
"CODE_NAME": "EVENT_SAVED",
"EVENT_TIMESTAMP": "2015-08-18T00:58:12.421+08:00"
}
],
"MSG_HEADER": {
"BUSINESS_OBJ_TYPE": "order",
"MSG_ID": "f79a672b-f15e-459d-a29b-725486d6401f",
"DESTINATIONS": "3"
}
},
"id": "0de3117e-12dd-4d10-a464-dff391a4513f"
}
Here, I am trying to Update a new event inside my event code
{
"CODE_NAME": "MESSAGE_DELIVERED_TO_APP2",
"EVENT_TIMESTAMP": "2015-08-18T12:58:12.421+08:00"
}
My final JSON will look like below,
{
"LOG_EVENT": {
"ATTRIBUTES": [
{
"ATTRIBUTE1": "TYPE",
"VALUE": "ORDER"
},
{
"ATTRIBUTE2": "NUMBER",
"VALUE": "1234567"
}
],
"EVENT_CODE": [
{
"CODE_NAME": "EVENT_SAVED",
"EVENT_TIMESTAMP": "2015-08-18T00:58:12.421+08:00"
},
{
"CODE_NAME": "MESSAGE_DELIVERED_TO_APP2",
"EVENT_TIMESTAMP": "2015-08-18T12:58:12.421+08:00"
}
],
"MSG_HEADER": {
"BUSINESS_OBJ_TYPE": "order",
"MSG_ID": "f79a672b-f15e-459d-a29b-725486d6401f",
"DESTINATIONS": "3"
}
},
"id": "0de3117e-12dd-4d10-a464-dff391a4513f"
}
Can you help on the ReQL query ?
Tried below, but not working
r.db("test").table("test1").get("0de3117e-12dd-4d10-a464-dff391a4513f")("LOG_EVENT")('EVENT_CODE').update(function(row) {
return {EVENT_CODE: row('EVENT_CODE').map(function(d) {
return r.branch(d.append({
"CODE_NAME": "MESSAGE_DELIVERED_TO_APP2",
"EVENT_TIMESTAMP": "2015-08-18T00:58:12.421+08:00"
}), d)
})
}} )

well here is the code which updates the nested fields of object residing inside an array
r.db('DB').table('LOGS')
.get('ID')
.update({
EVENT_CODE: r.row('EVENT_CODE')
.changeAt(1, r.row('EVENT_CODE').nth(1)
.merge({"CODE_NAME": "MESSAGE_DELIVERED_TO_APP2"}))
})

Related

Mongo Query to modify the existing field value with new value + array of objects

I want to update many documents based on the condition in MongoDB.
MODEL is the collection which has the document with below information.
"info": [
{
"field1": "String1",
"field2": "String2"
},
{
"field1": "String1",
"field2": "String_2"
}
],
"var": "x"
I need to update all the "String1" value of field1 with "STRING_NEW". I used the below query to update but not working as expected.
db.model.updateMany(
{ "info.field1": { $exists: true } },
[
{ "$set": {
"info": {
"$map": {
"input": "$info.field1",
"in": {
"$cond": [
{ "$eq": ["$$this.field1", "String1"] },
"STRING_NEW",
$$this.field1
]
}
}
}
} }
]
)
Please have a look and suggest if anything is to be modified in the above query.
Solution 1
With the update with aggregation pipeline, you should iterate the object in info array and update the iterated object by merging the current object with new field1 field via $mergeObjects.
db.model.updateMany({
"info.field1": "String1"
},
[
{
"$set": {
"info": {
"$map": {
"input": "$info",
"in": {
"$cond": [
{
"$eq": [
"$$this.field1",
"String1"
]
},
{
$mergeObjects: [
"$$this",
{
"field1": "STRING_NEW"
}
]
},
"$$this"
]
}
}
}
}
}
])
Demo Solution 1 # Mongo Playground
Solution 2
Can also work with $[<identifier>] positional filtered operator and arrayFilters.
db.model.updateMany({
"info.field1": "String1"
},
{
"$set": {
"info.$[info].field1": "STRING_NEW"
}
},
{
arrayFilters: [
{
"info.field1": "String1"
}
]
})
Demo Solution 2 # Mongo Playground

How to filter JSON data based on another JSON data in typescript

I have 2 JSON Data 1. Payers 2. Rules. I need to filter Payers JSON data based on PayerId from Rules JSON data.
{
"Payers": [
{
"payerId": "12345",
"name": "Test Payer1"
},
{
"payerId": "23456",
"name": "Test Payer2",
},
{
"payerId": "34567",
"name": "Test Payer3"
}}
Rules JSON file
{
"Rules": [
{
"actions": {
"canCopyRule": true
},
"RuleId": 123,
"description": "Test Rule",
"isDisabled": false,
"Criteria": [
{
"autoSecondaryCriteriaId": 8888,
"criteriaType": { "code": "primaryPayer", "value": "Primary Payer" },
"payerId": ["12345", "34567"]
}
]
}
}]}
I need to filter Payers JSON data based on Rules JSON data if PayerID matches
I need output like below
{
"Payers": [
{
"payerId": "12345",
"name": "Test Payer1"
},
{
"payerId": "34567",
"name": "Test Payer3"
}
}
How to filter?
You can use Array.filter like that (based on your data structure):
const filteredPayers = payersObj.Payers.filter((p) => rulesObj.Rules[0].Criteria[0].payerId.includes(p.payerId));
I can't figure out why your Rules json looks like this, I guess you have multiple rules. If so, you will need to iterate over each rule and invoke includes. Same for Criteria.
Code will check each rule and each critirias
and will return payers if payerId found in any of the given rules of any criteria
const payers = {
"Payers": [
{
"payerId": "12345",
"name": "Test Payer1"
},
{
"payerId": "23456",
"name": "Test Payer2",
},
{
"payerId": "34567",
"name": "Test Payer3"
}]}
const rules = {
"Rules": [
{
"actions": {
"canCopyRule": true
},
"RuleId": 123,
"description": "Test Rule",
"isDisabled": false,
"Criteria": [
{
"autoSecondaryCriteriaId": 8888,
"criteriaType": { "code": "primaryPayer", "value": "Primary Payer" },
"payerId": ["12345", "34567"]
}
]
}
]
}
const data = payers.Payers.filter(payer => rules.Rules.findIndex(rule => rule.Criteria.findIndex(criteria => criteria.payerId.includes(payer.payerId)) != -1) !== -1)
console.log(data)

how to create specific array in jolt

im in learning process to learn jolt, but quite hard to master as there is array and the output must be the exactly the same as example below.
how to create a jolt spec form ,
the json input is like this :
[
{
"encounter_date": "1616509603296",
"id_no": "671223025051",
"patient_id": "MAEPS-PID-2100003716",
"patient_mrn": "MAEPS-MRN-2100003815",
"first_name": "MOHD RAZALI "
},
{
"encounter_date": "1621324591194",
"id_no": "950224145647",
"patient_id": "MAEPS-PID-2100030302",
"patient_mrn": "MAEPS-MRN-2100030401",
"first_name": "MUHAMMAD FADDIL BIN YASIN"
}
]
expected output is like this :
{
"forms": [
{
"visit": {
"patientId": "MAEPS-PID-2100003716",
"Patientmrn": "MAEPS-MRN-2100003815",
"encounterDate": "2021-03-23 22:26:43.296"
},
"person": {
"firstname": "MOHD RAZALI ",
"identifications": [
{
"idNo": "671223025051"
}
]
}
},
{
"visit": {
"patientId": "MAEPS-PID-2100030302",
"Patientmrn": "MAEPS-MRN-2100030401",
"encounterDate": "2021-05-18 15:56:31.194"
},
"person": {
"firstname": "MUHAMMAD FADDIL BIN YASIN",
"identifications": [
{
"idNo": "950224145647"
}
]
}
}
]
}
i'm new to jolt and require guidance
This can be done with just a single shift operation as below.
[
{
"operation": "shift",
"spec": {
"*": {
"patient_id": "forms[&1].visit.patientId",
"patient_mrn": "forms[&1].visit.Patientmrn",
"encounter_date": "forms[&1].visit.encounterDate",
"first_name": "forms[&1].person.firstname",
"id_no": "forms[&1].person.identifications[0].idNo"
}
}
}
]

How to delete all sub array

In my MongoDB database, I have a collection 'produits' with documents like this
{
"_id": {
"$oid": "6048e97b4a5f000096007505"
},
"modeles": [
{
"id": "OppoA3",
"pieces": [
{
"id": "OppoA3avn"
},
{
"id": "OppoA3bat"
}]
]
},
{
"id": "OppoA1",
"pieces": [
{
"id": "OppoA1avn",
},
{
"id": "OppoA1batt",
}
]
}
]
}
How can I delete all modeles.pieces from all my documents.
I managed to delete with a filter on modeles.id but with that code but not on all the collection
db.produits.update(
{marque_id:'OPPO', 'modeles.id':'RENOZ'},
{$set:
{
'modeles.$.pieces': []
}
}
, { multi : true }
)
I would like all documents like this finally
{
"_id": {
"$oid": "6048e97b4a5f000096007505"
},
"modeles": [
{
"id": "OppoA3",
"pieces": []
},
{
"id": "OppoA1",
"pieces": []
}
]
}
Thank you for your help.
I have done a javascript loop like this, but i think it's not best practice
async removePieces(){
var doc
try {
doc = await produitModel.find()
for (var produit of doc) {
for (var modele of produit.modeles) {
const filter = {'marque_id': produit.marque_id, 'modeles.id': modele.id}
const set = {
$set: {
'modeles.$.pieces': []
}
}
await db.collection('produits').updateOne(filter, set)
}
}
console.log('removePieces() ==> Terminé')
} catch(err) {
console.log(err)
}
}
db.produits.update({
modeles: {//This is because your second document will create failure otherwise
$exists: true
}
},
{
$set: {
"modeles.$.pieces": []
}
},
{
multi: true
})

Nodejs Mongoose select a entire document but only with selected items in a property array

I'm new to nodejs and currently i'm developing a Web API using nodejs, express and mongoose. Can anyone help me on following mongoose query.
var EventSchema = new Schema({
event_title: {
type: String,
required: true
},
service_order: [{
_id:{
type: String
},
service : {
type: Schema.Types.ObjectId,
ref: "Service"
}
}]
});
I want to select a entire Event document but only with selected service_order items in that array
ex :-
this is a entire Event document
{
_id: "sample_id",
name: 'some name',
"service_order": [
{
"_id": "1"
"service": {
"_id": "59c005524d9c141fe0d95f15"
},
{
"_id": "2"
"service": {
"_id": "59c005524d9c141fe0d95f18"
},
{
"_id": "3"
"service": {
"_id": "59c005524d9c141fe0d95f18"
},
{
"_id": "4"
"service": {
"_id": "59c005524d9c141fe0d95f18"
}
],
}
But I want to execute a single mongoose query which can give this as the output
{
_id: "sample_id",
name: 'some name',
"service_order": [
{
"_id": "1"
"service": {
"_id": "59c005524d9c141fe0d95f15"
},
{
"_id": "2"
"service": {
"_id": "59c005524d9c141fe0d95f18"
}
],
}
Initially I know the Event id ("sample_id") and the ids of service_orders that i want to select ( ["1","2"] ).
You can use mongodb aggregate to achieve that
don't forget to replace sample_id and ["1" , "2"] with your dynamic data
Check the code below:
db.event.aggregate([
{
$match: { _id: "sample_id" }
},
{
$unwind: { path: "$service_order" }
},
{
$match : { 'service_order._id' : { $in : [ "1" , "2"]} }
},
{
$group : {
_id: { _id: "$_id", "name": "$name"},
service_order: { $addToSet: "$service_order" },
}
},
{
$project : {
_id : "$_id._id",
name : "$_id.name",
service_order : 1
}
}
])

Resources