Nodejs Mongodb OR condition - node.js

I want to search "search" parameter in headers, api_data, ip_address, tarih_2
where am i doing wrong? Help me please.
{
"$and":[
{
"$or":[
{
"headers":{
"$regex":".*94.54.232.96.*"
}
},
{
"api_data":{
"$regex":".*94.54.232.96.*"
}
},
{
"ip_address":{
"$regex":".*94.54.232.96.*"
}
},
{
"tarih_2":{
"$regex":".*94.54.232.96.*"
}
}
]
}
]
}
no problem with query. Thank you.

Related

findOneAndUpdate in mongoose

I have a json documnent lit this:
{ "mainData": { "id": "2131231312.." "data": { "oneNode": { ... } "otherNode": { ... } "anyNode": { .... } } }
In PATCH someone send me:
{
"mainData": {
"id" "2131231312.."
"oneNode": {
"field1": "value"
}
}
}
Trying to use findOneAndUpdate() to change an object inside subnode.
I'd like to change a value in "oneNode" or create node "oneNode" in "data" is not presente.
Any suggestions?
Thanks.
I use
DBValue.findOneAndUpdate({ "mainData.id": instanceId }, { "mainData": { "id" "2131231312..", "oneNode": { "field1": "value"}})
If I understand your question correctly, you can use the $set operator to only set specific points of data to the document. It can be used to update existing values OR to add new values.
https://www.mongodb.com/docs/manual/reference/operator/update/set/
Perhaps something like this:
DBValue.findOneAndUpdate(
{ "mainData.id": instanceId },
{
$set: {
'mainData.someExistingNode.someField': 1234,
'mainData.someNewField': 22
}
}
)

How to use filter expressions on aws using python3 for nested map attribute?

I have been trying to scan DynamoDB to check for particular value in a nested map attribute named deliverables. However using scan with filter expressions is resulting in an empty result.
import boto3
result = []
dynamo_client = boto3.client("dynamodb")
paginator = dynamo_client.get_paginator("scan")
operation_parameters = {
'FilterExpression': "#Deliverable= :deliverable",
'ExpressionAttributeNames': {
'#Deliverable': 'deliverables.fc986523-a666-478e-8303-2a1c3c1dc4ba'
},
'ExpressionAttributeValues': {
':deliverable': {
"M": {
"read": {
"BOOL": True
},
"upload": {
"BOOL": True
},
"write": {
"BOOL": True
}
}
}
}
}
for page in paginator.paginate(TableName="TableName", **operation_parameters):
result.append(page["Items"])
print(result)
The items in the dynamo db look like this:
[
[
{
"deliverables":{
"M":{
"7397d832-fefb-4ba2-97a1-0f6e73d611d9":{
"M":{
"read":{
"BOOL":true
},
"upload":{
"BOOL":true
},
"write":{
"BOOL":true
}
}
},
"fc986523-a666-478e-8303-2a1c3c1dc4ba":{
"M":{
"read":{
"BOOL":true
},
"upload":{
"BOOL":true
},
"write":{
"BOOL":true
}
}
}
}
},
"username":{
"S":"username1"
},
"deniedReferences":{
"L":[
]
}
},
{
"deliverables":{
"M":{
"7397d832-fefb-4ba2-97a1-0f6e73d611d9":{
"M":{
"read":{
"BOOL":true
},
"upload":{
"BOOL":false
},
"write":{
"BOOL":false
}
}
},
"fc986523-a666-478e-8303-2a1c3c1dc4ba":{
"M":{
"read":{
"BOOL":true
},
"upload":{
"BOOL":false
},
"write":{
"BOOL":false
}
}
}
}
},
"username":{
"S":"repositoryadmin"
},
"deniedReferences":{
"L":[
]
}
}
]
]
Please let me know if you can help me solve this issue.
The problem is the [dot] here: 'ExpressionAttributeNames': { '#Deliverable': 'deliverables.fc986523-a666-478e-8303-2a1c3c1dc4ba'}
Expressions docs: DynamoDB interprets a dot in an expression attribute name as a character within an attribute's name.
operation_parameters = {
"FilterExpression": "#D0.#D1=:deliverable", # the dot goes here!
"ExpressionAttributeNames": {
"#D0": "deliverables",
"#D1": "fc986523-a666-478e-8303-2a1c3c1dc4ba"
},

parse json in nodejs with : in the json keys

My nodejs code receives a response from a HTTP POST call which is in xml. I convert it to json using xml2js, and now I need to read this so as to get the data of one of the json keys.
This is how a part of my json data looks. I am trying to read this as follows:
var base64encoded = jsonxml."soapenv:Body".runReportResponse.runReportReturn.reportBytes;
However, when I run this i get the error message:
jsonxml."soapenv:Body".runReportResponse.runReportReturn.reportBytes;
^^^^^^^^^^^^^^
SyntaxError: Unexpected string.'
I have also tried by removing the double quotes, but then it gives exception to the colon (:) which appears in the data. How do I read such kind of data?
Entire json:
[
{
"Envelope": {
"$": {
"xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
},
"Body": {
"runReportResponse": {
"$": {
"xmlns": "http://xmlns.oracle.com/oxp/service/PublicReportService"
},
"runReportReturn": {
"metaDataList": {
"$": {
"xsi:nil": "true"
}
},
"reportBytes": "MzAwMDAwMDA0Mzk5ODEwLERDT0cgQ29ycG9yYXRlIEJVDQozMDAwMDAwMDk0ODE4MzEsREVMRlRMQUJfVVNfQlVTSU5FU1NfVU5JVA0KMzAwMDAwMDAzMDYyNTI1LERFTEhJVEVDSF9VU19CVVNJTkVTU19VTklUDQozMDAwMDAwMDMwNjE1ODMsREVMTFMgVVMgQlUNCjMwMDAwMDAwMzE3OTE0NixERUxNRkcgVVMgQlUNCjMwMDAwMDAxMDI1NDA1NyxESEMgQ29ycG9yYXRlDQo=",
"reportContentType": "text/plain;charset=UTF-8",
"reportFileID": {
"$": {
"xsi:nil": "true"
}
},
"reportLocale": {
"$": {
"xsi:nil": "true"
}
}
}
}
}
}
}
]
I've updated the answer base on your new JSON (thank you!). It looks like the problem is maybe that each property in the JSON is really an array.
I'd suggest if you don't want this behaviour, to have a look at the explicitArray options in the xml2js options object setting it to false.
e.g. :
explicitArray (default: true): Always put child nodes in an array if true; otherwise an array is created only if there is more than one.
This might simplify accessing properties!
let response = [
{
"Envelope": {
"$": {
"xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
},
"Body": {
"runReportResponse": {
"$": {
"xmlns": "http://xmlns.oracle.com/oxp/service/PublicReportService"
},
"runReportReturn": {
"metaDataList": {
"$": {
"xsi:nil": "true"
}
},
"reportBytes": "MzAwMDAwMDA0Mzk5ODEwLERDT0cgQ29ycG9yYXRlIEJVDQozMDAwMDAwMDk0ODE4MzEsREVMRlRMQUJfVVNfQlVTSU5FU1NfVU5JVA0KMzAwMDAwMDAzMDYyNTI1LERFTEhJVEVDSF9VU19CVVNJTkVTU19VTklUDQozMDAwMDAwMDMwNjE1ODMsREVMTFMgVVMgQlUNCjMwMDAwMDAwMzE3OTE0NixERUxNRkcgVVMgQlUNCjMwMDAwMDAxMDI1NDA1NyxESEMgQ29ycG9yYXRlDQo=",
"reportContentType": "text/plain;charset=UTF-8",
"reportFileID": {
"$": {
"xsi:nil": "true"
}
},
"reportLocale": {
"$": {
"xsi:nil": "true"
}
}
}
}
}
}
}
];
console.log("Report bytes: ", response[0].Envelope.Body.runReportResponse.runReportReturn.reportBytes);

MongoDB: Regroup by two field with different value

I created a nodejs app in Typescript.
I want to group documents by two fields "one_id" and "two_id" with specified one_id value.
Here is my data in my collection:
{
"_id":"5a8b2953007a1922f00124fd",
"one_id":"307973260186877954",
"two_id":"415228402765660181"
}
{
"_id":"5a8b29a3007a1922f00124fe",
"one_id":"415228402765660181",
"two_id":"307973260186877954"
}
{
"_id":"5a8c119bf6ba49302c3ef67e",
"one_id":"394199132195127306",
"two_id":"270131587092316161"
}
{
"_id":"5a8c11a4f6ba49302c3ef67f",
"one_id":"270131587092316161",
"two_id":"394199132195127306"
}
{
"_id":"5a8c33132a182308a836bc1c",
"one_id":"307973260186877954",
"two_id":"397036401075552256"
}
{
"_id":"5a8c33242a182308a836bc1d",
"one_id":"397036401075552256",
"two_id":"307973260186877954"
}
And if I want to get pairs with one_id="307973260186877954", the excepted result would be: (must have another document with "inversed" fields content)
{
"_id":"5a8b2953007a1922f00124fd",
"one_id":"307973260186877954",
"two_id":"415228402765660181"
}
{
"_id":"5a8b29a3007a1922f00124fe",
"one_id":"415228402765660181",
"two_id":"307973260186877954"
}
{
"_id":"5a8c33132a182308a836bc1c",
"one_id":"307973260186877954",
"two_id":"397036401075552256"
}
{
"_id":"5a8c33242a182308a836bc1d",
"one_id":"397036401075552256",
"two_id":"307973260186877954"
}
I don't know if you understand me.
Thank you, I hope someone will understand me!
You can use $lookup to self join the rows and output the document when there is a match and $project with exclusion to drop the joined field.
db.col.aggregate([
{"$lookup":{
"from":col,
"localField":"one_id",
"foreignField":"two_id",
"as":"onetwo"
}},
{"$lookup":{
"from":col,
"localField":"two_id",
"foreignField":"one_id",
"as":"twoone"
}},
{"$match":{"onetwo.0":{"$exists":true}, "twoone.0":{"$exists":true}}},
{"$project":{"onetwo":0,"twoone":0}}
])
I think this should be your solution. Not sure. Just gave it a try
db.TempCollections.aggregate([
{
$lookup:{
from:"TempCollections",
let:{
leftone_id:"$one_id",
lefttwo_id:"$two_id"
},
pipeline:[
{
$match: {
$expr: {
$and: [
{
$eq: [
"$one_id",
"$$lefttwo_id"
]
},
{
$eq: [
"$two_id",
"$$leftone_id"
]
}
]
}
}
}
],
as:"Final"
}
}
],
{
allowDiskUse:true
});

MongoDb nested Array retrieve

i am using mongodb 2.6
my schema
{
list:[
{ Sno:1,
sublist:[
{
subid:"1"
dats:"result"
},
{
subid:"2"
dats:"result"
}
]
},
{ Sno:2,
sublist:[
{
subid:"3"
dats:"result"
},
{
subid:"4"
dats:"result"
}
]
}
]
}
db.test.find({"list.sublist.subid": 2});
i want to return only matched data like this
i don't know how to use mapreduce for this solution
{
sublist:[
{
subid:"1"
dats:"result"
}
]
}
You will need to use a projection http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/
In your case try with $elemMatch (http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/#proj._S_elemMatch)
db.test.find( {},
{ sublist: { $elemMatch: { subid: "1" } } } )

Resources