How to get only part of the document while querying with mongoose? - json

I have following
{
"_id" : ObjectId("56d808d826941f84bc697b4e"),
"outerArray" : [
{
"middleArray" : [
{
"someKey": "value",
"innerArray" : [
"F999-99999",
"F999-999999",
"FAF-99999",
"FSF-99999",
"FSW-99999",
"FSX-99999",
"FAF-999999",
"FSF-999999",
"FSW-999999",
"FSX-999999"
]
}
]
}
]
}
when I query
Model.find({outerArray.middleArray.someKey = 'value'},outerArray.middleArray.innerArray)},function(err,results){
//do some thing
}
I am getting innerArray along with the outer structure. But what i want is only innerArray part like -
"innerArray" : [
"F999-99999",
"F999-999999",
"FAF-99999",
"FSF-99999",
"FSW-99999",
"FSX-99999",
"FAF-999999",
"FSF-999999",
"FSW-999999",
"FSX-999999"
]
so i dont need to traverse through all outer arrays like outerArray[0].middleArray[0].innerArray
I looked into projections but i am not able to find anything. Please help me with this. Thanks a Lot ...

how about this?
var _ = require('lodash');
var data = {
"_id" : "SOMETHING",
"outerArray" : [
{
"middleArray" : [
{
"someKey": "value",
"innerArray" : [
"F999-99999",
"F999-999999",
"FAF-99999",
"FSF-99999",
"FSW-99999",
"FSX-99999",
"FAF-999999",
"FSF-999999",
"FSW-999999",
"FSX-999999"
]
}
]
}
]
}
data = _.result(data, "outerArray[0].middleArray[0].innerArray", null);
console.log(data);
Also have You tried this ?
Model.find(
{outerArray.middleArray.someKey: 'value'},
'outerArray.middleArray.innerArray',
function(err,results){
console.log(results);
});

You can't do what you want, directly.
MongoDB is a document database, not a relational database. When you retrieve (find) a document, it will retrieve the entire document, always.
If you only want the inner array, you need to pull that out of the full document that was returned to you.

Related

React state and JSON and arrays => faulty result

Edit: So, I found the solution to my initial question, which made me realize I have another issue.
It seemed to be easier than I thought
setNumbers(e) {
e.preventDefault();
var already_exists = false;
var ls_data = this.state.storedNumbers;
var rname = document.getElementById('name').value;
var rnumb = document.getElementById('nummer').value;
var ls_key = this.state.ls_key;
for (key in ls_data) {
if(ls_data.hasOwnProperty(key) === true) {
if(ls_data[key].name === rname) {
if(ls_data[key].Number === rnumb) {
already_exists = true;
}
}
}
}
if(!already_exists) {
ls_key++;
ls_data[ls_key] = {
name: rname,
Number: rnumb
};
localStorage.setItem("ls_numbers", JSON.stringify(this.state.storedNumbers));
localStorage.setItem("ls_key", ls_key);
this.setState({
ls_key: localStorage.getItem("ls_key"),
});
}
}
But now my issue is, that I can't iterate over it, because it is a nested object and not an array. So .map will not work (this.state.storedNumbers.map is not a function).
Changing storedNumber to an array sadly doesn't solve the issue, as
ls_data[ls_key] = {
name: rname,
Number: rnumb
};
isn't working in an array. So now my question is. Can I use my ls_key variable to create a name object in my array? Using the code above results in:
[
null,
{
"name" : "Person 1",
"Number" : "XXXXXXXX"
},
{
"name" : "Person 2",
"Number" : "XXXXXXXX"
}
]
while the array should look like:
[
"1": {
"name" : "Person 1",
"Number" : "XXXXXXXX"
},
"2": {
"name" : "Person 2",
"Number" : "XXXXXXXX"
}
]
Or is there a way to iterate over the nested JSON result, as .map does for an array?
Alright then, just figured it out myself:
The reason my data got malformed (initial question), still in the blue about that. I've changed a lot of code and reverted to the original code again, et voila, miraculously it all worked. Can't tell you what the difference was. After that I could easily simplify the code as shown in the edited question.
To iterate over the data, the code below was my solution. Should you have a more cleaner solution, let me know!
{this.state.storedNumbers.length < 1
? <li className='list-group-item'><strong>Geen recente nummers...</strong><span className='pull-right'><span className='glyphicon glyphicon-warning glyph-xl'></span></span></li>
: Object.keys(this.state.storedNumbers).map((number) => {
return (
<div className='item' key={number}>
<a className='list-group-item list-link'>
<strong>{this.state.storedNumbers[number].name}</strong> - ({this.state.storedNumbers[number].Number})
</a>
<span className='button-delete glyph-xl'>
<i className='glyphicon glyphicon-trash glyph-xl' aria-hidden='true'></i>
</span>
</div>
)})
}

Need help in Append JSON file using ContentMerge in Apache NIFi

I'm trying to merge a JSON file which has multiple objects. Below is my Oringinal JSON file.
{
"applicant": {
"full-name": "Tyrion Lannister",
"mobile-number" : "8435739739",
"email-id" : "tyrionlannister_casterlyrock#gmail.com"
},
"product": {
"product-category" : "Credit Card",
"product-type" : "Super Value Card - Titanium"
}
}
I will get some more JSON data as below from other source.
{
"flags": {
"duplicate-flag" : "No"
"contact-flag" : "Yes"
}
}
My task is to append the new JSON in the old JSON recods as a new object as below.
{
"applicant": {
"full-name": "Tyrion Lannister",
"mobile-number" : "8435739739",
"email-id" : "tyrionlannister_casterlyrock#gmail.com"
},
"product": {
"product-category" : "Credit Card",
"product-type" : "Super Value Card - Titanium"
},
"flags": {
"duplicate-flag" : "No"
"contact-flag" : "Yes"
}
}
Can someone help to guide, how it can be achieved in NiFi ?
I recommend accumulating your components as flowfile attributes, then forming a merged object with an ExecuteScript processor using JavaScript/ECMAScript. Sometimes there's just no substitute for JavaScript. Something like the following might work:
flowFile = session.get();
if (flowFile != null) {
var OutputStreamCallback = Java.type("org.apache.nifi.processor.io.OutputStreamCallback");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
// Get attributes
var applicant = JSON.parse(flowFile.getAttribute("applicant"));
var product = JSON.parse(flowFile.getAttribute("product"));
var flags = JSON.parse(flowFile.getAttribute("flags"));
// Combine
var merged = {
"applicant": applicant,
"product": product,
"flags": flags
};
// Write output content
flowFile = session.write(flowFile, new OutputStreamCallback(function(outputStream) {
outputStream.write(JSON.stringify(merged, null, "\t").getBytes(StandardCharsets.UTF_8));
}));
session.transfer(flowFile, REL_SUCCESS);
}

Firebase + Aurelia: how to process the returned key=>value format by Firebase?

I'm retrieving the following structure from Firebase:
"bills" : {
"1" : { // the customer id
"orders" : {
"-KVMs10xKfNdh_vLLj_k" : [ { // auto generated
"products" : [ {
"amount" : 3,
"name" : "Cappuccino",
"price" : 2.6
} ],
"time" : "00:15:14"
} ]
}
}
}
I'm looking for a way to process this with Aurelia. I've written a value converter that allows my repeat.for to loop the object keys of orders, sending each order to an order-details component. The problem is, this doesn't pass the key, which I need for deleting a certain order ("-KVMs10xKfNdh_vLLj_k")
Should I loop over each order and add the key as an attribute myself?
Is there a better/faster way?
This answer might be a little late (sorry OP), but for anyone else looking for a solution you can convert the snapshot to an array that you can iterate in your Aurelia views using a repeat.for, for example.
This is a function that I use in all of my Aurelia + Firebase applications:
export const snapshotToArray = (snapshot) => {
const returnArr = [];
snapshot.forEach((childSnapshot) => {
const item = childSnapshot.val();
item.uid = childSnapshot.key;
returnArr.push(item);
});
return returnArr;
};
You would use it like this:
firebase.database().ref(`/bills`)
.once('value')
.then((snapshot) => {
const arr = snapshotToArray(snapshot);
});

NodeJS JSON.parse convert the char ! into?

i have a problem with JSON.parse() in a NodeJS programm usging Express.
The problem is that when i get an external json using http.get for example like this:
[ { "Name" : "Parachutes", "Artist" : "Coldplay"}, { "Name" : "Lost!", "Artist": "Coldplay" } ]
When i do JSON.parse(ResponseStr) the i get the following json
[ { "Name" : "Parachutes", "Artist" : "Coldplay"}, { "Name" : "Lost?", "Artist": "Coldplay" } ]
The char ! is converting into ? when i do that JSON.parse, i want to have the same output as the original Name because when i want to use that name in a http.get('/example.com/album/Lost?') it is returning to me a 404 error.
This is mi my code:
function httpget(url,callback)
{
var str = "";
http.get(url, function(resp){
resp.on('data', function(chunk){
str += chunk;
});
resp.on('end', function(){
var obj = JSON2.parse(str);
callback(obj);
});
});
}
Edit 1:
I already using resp.setEncoding('utf8'); in my code, the problem is in in the JSON2.parse() (is same as JSON.parse()), before that i can do console.log(str) and the output will show "Lost!" but when i do JSON.parse() the output is like this "Lost?"

How to convert a MongoDB document to JSON Object

I am trying to make a post request with the MongoDB document returned from find query, as the request body in NodeJS.But on the server I'm getting the Error : Invalid JSON. Below is the document that I'm trying to POST
{
"_id" : ObjectId("5739a6bf3f1b41477570dc89"),
"taskCount" : 2,
"study" : "cod",
"phase" : "mansa2",
"rhimeTaskId" : "5739a6bec4567f6e737fd3db",
"recordId" : "5726f3cfc4567f6e737fc3ab",
"recordStudy" : "codstudy",
"recordPhase" : "mansa2",
"recordLanguage" : "Punjabi",
"recordScript" : "Latin",
"_state" : "CodingComplete",
"tasks" : [
{
"physician" : ObjectId("5739a6bd3f1b41477570dc78"),
"stage" : "Coding",
"result" : {
"cod" : "C15",
"feedback" : {
"narrativeLength" : "Adequate",
"positiveSymptomsIncluded" : "Only Positive",
"certainty" : "High"
},
"keywords" : [
"52 yr male, died of food pipe cancer, suffered pain upper abdomen, investigated,FNAC confirmed Cancer, Put on Chemotherapy, multiple cycles, died at home, had fever with chills occasionally"
]
}
},
{
"physician" : ObjectId("5739a6bd3f1b41477570dc79"),
"stage" : "Coding",
"result" : {
"cod" : "C15",
"feedback" : {
"narrativeLength" : "Inadequate",
"positiveSymptomsIncluded" : "Only Positive",
"certainty" : "High"
},
"keywords" : [
"severe pain abdomen, ultrasonography revealed food pipe cancer, chemotherapy given, died"
]
}
}
],
"__v" : 2
}
and here is the code that I wrote to make the POST request
var MongoClient = require('mongodb').MongoClient;
var request = require('request');
var assert = require('assert');
var cmeprovisioning= 'mongodb://localhost:27017/cmeprovisioning';
MongoClient.connect(cmeprovisioning, function(err, db) {
assert.equal(null, err);
var count=0;
console.log("Connected to cmeprovisioning");
var cursor =db.collection('rhimeReport').find(
{"study":"cod","phase":"mansa2","recordStudy":"codstudy",
"recordPhase":"mansa2","_state":"CodingComplete"
});
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
console.dir(doc);
count=count+1;
request({url: "http://cme.host.net:8081/cme-provisioning/update",
method: "POST",json: true,
headers: {"content-type": "application/json"},
json: doc
},function(e,r,b){
console.log("POST Error "+count+" "+e)
console.log("POST Response "+count+" "+r)
console.log("POST BODY "+count+" "+b)
});
} else {
console.log("Some Error : "+err)
}
});
});
I also tried using JSON.stringify(doc), but still got the Invalid JSON error. Is there a way I can use mongo document returned by the find query and convert it to JSON to make the POST request.
I think those ObjectID is what making it an invalid JSON document.
Here's the actual answer:
If you want to convert a mongo object to JSON object.
There's a utility method in every mongo object toJSON
So you can simply do mongoResponseObject.toJSON() on the response object.
e.g.
Products.findById(id).then(res => {
const jsonRes = res.toJSON();
// Here jsonRes is JSON
})
Alternatively you can directly get the JSON object by using the .lean() like this.
Products.findById(id).lean().then(res => {
// Here res is JSON
})
you need to convert object id to string ie.
var result = {
"_id": ObjectId("5739a6bf3f1b41477570dc89"),
"taskCount": 2,
"study": "cod"
};
//now convert to string
result=result._id.toString();
//now you can use the result
Try this,
var cursor =db.collection('rhimeReport').find(
{"study":"cod","phase":"mansa2","recordStudy":"codstudy",
"recordPhase":"mansa2","_state":"CodingComplete"});
cursor.toString();
......
Hope this help.
Try this in robomongo
var cursor = db.getCollection('X').find({},{})
while(cursor.hasNext()) {
print(JSON.stringify(cursor.next()))
}