MongoDB functions with date query - mongodb

How does one write a JSON with Data Queries? I am building a mongodb function to accept HTTP request and parse query. I’ve tried new Date(“2016-10-11T00:00:00Z”) and ISO date function. I always end up with JSON parse errors as it can’t accept functions. How can we write a dynamic query string which can be sent in requestbody of http call? Thanks.
{
"query": {"account.companyName":"Groups","_createdAt":{"$gte":ISODate("2016-10-11T00:00:00Z")}},
"projection": {
"crn": 1,
"state": 1
1 }
}
;
t= JSON.parse(body);
var res= collection.find(t.query,t.projection,function(err, cursor){
cursor.toArray(callback);
db.close();
});

Related

Max item count in Cosmos DB trigger

I'm creating a pre-trigger for a Cosmos DB container. The pre-trigger is supposed to fetch all data related to the triggering document id. The incoming_document.items is always returning 100 when there are more than 100 documents expected (which seems to be limited by the query). I tried to set the pageSize property to -1 in the FeedOptions parameters and to use continuation, but it is still giving me 100. How can I fix this to give the total count?
Here is a simplified version of the code (without the continuation, I used a similar code to here):
function trgAddStats() {
var context = getContext();
var request = context.getRequest();
var incoming_document = request.getBody();
var container = context.getCollection();
var incoming_document.items = 1;
var filterQuery = {
"query": `SELECT t.customer, t.amount FROM Transactions_ds t WHERE t.customer = #customer`,
"parameters": [{
"name": "#customer",
"value": incoming_document.customer
}
]
};
var isAccepted = container.queryDocuments(container.getSelfLink(), filterQuery, {},
function (err, items, responseOptions) {
if (err) throw new Error("Error" + err.message);
incoming_document.items += items.length;
request.setBody(incoming_document);
}
);
if (!isAccepted) throw "Unable to update transaction, abort";
}
For getting more than 100 documents in Cosmos DB we can make use of x-ms-max-item-count.
The maximum number of values that can be returned by the query execution is done by the x-ms-max-item-count header.
The default value of the query results is 100 and it can be configured from 1–1000 using this header.
For more details regarding Pagination of query results in Microsoft Documentation.
You can Customize the number for Items per page in Query Explorer too like here.

Unknown bson type DECIMAL128 in when show on template client?

I user find for meteor and aggregate I try to get data form server I test both of them it work but, I get trouble when I use
.toNumber() function with aggregate that data type amount is Decimal in mongodb. I am not sure to get data to show on client in template. any suggestion Please help me
My Code collection Schema
Collection schema (SimpleSchema)
amount: {
type: Object, // I tried use `Decimal`
blackbox: true,
},
Method on server sample it work find after insert amount store in data type Decimal and get data both with findOne abd aggregate
import { Decimal } from 'meteor/mongo-decimal'
Meteor.methods({
insert(){
return Collection.insert({amount: Decimal(0.123)})
},
findOne(){
return Collection.findOne({})
},
aggregate(){
return Collection.aggregate([{ $limit: 1 }])[0] // sakulstra:aggregate
}
})
Get data in Client when get with findOne .toNumber() it work fine but when I use with aggregate .toNumber() it unknown.
So during find query I am getting Unknown bson type DECIMAL128
Meteor.call('findOne', (err, res)=>{
res.amount.toNumber() // Work fine with Decimal JS method
})
Meteor.call('aggregate', (err, res)=>{
res.amount.toNumber() // don't work with Decimal JS method
})

Suppress value types in MongoDB Stitch function

A Stitch function returns value types for each non-string field. I believe this is because functions return data in MongoDB Extended JSON.
The Mongo Shell, on the other hand, returns standard JSON, and no value types.
How do I suppress value types returned by a MongoDB function? Is it possible to convert EJSON back to JSON?
For a date field, for example, the Mongo Shell returns:
"dob" : ISODate("1995-01-11T00:00:00.000-07:00")
The same query in a Stitch function returns:
"dob": {
"$date": {
"$numberLong": "232182000000"
}
My Stitch function looks like this:
exports = function(){
const collection = context.services.get("mongodb-atlas").db("mydb").collection("mycollection");
const doc = collection.find().toArray();
return doc;
};
Is there a helper function that can strip out the value types? Something like...
exports = function(){
const collection = context.services.get("mongodb-atlas").db("mydb").collection("mycollection");
const doc = collection.find().toArray();
const noValueTypes = doc.stripValueTypes()
return noValueTypes;
};
When a Function result is passed to the client, it is indeed serialized as EJSON.
Ideally, your client can parse EJSON back into regular JS objects, e.g. with the EJSON library, which is also built into the Stitch SDK.
Of course, if you are using the Stitch SDK, calling the function directly is even better.
Another option is to use the response object to pass JSON, like so:
exports = function(request, response) {
// ... get data ...
response.addHeader(
"Content-Type",
"application/json"
);
response.setBody(JSON.stringify(myData));
};
Note that JSON can't represent some special BSON types, such as object id, so you will want to keep this in mind when deciding what data to return.

How to split up Postgres data into JSON/object, or an easily usable format for Express

I'm using pg-promise to facilitate requests between Express and a Postgres database.
Using pg-promise's any:
db.any('select * from blog')
.then(function (data) {
console.log(data);
})
.catch(function (error) {
console.log('ERROR:', error);
});
There is only one row in the Postgres table, so the query above gets a block of data from Postgres that, when output to the terminal, looks like this:
[ { id: 1,
date: '2018-12-17',
title: 'Here is a Title',
article: "Okay, there's not much here yet.",
img1: null,
img2: null,
img3: null,
keywords: 'news' } ]
In an effort to break up this data into usable bits that I can then assign to values in Express, I attempted to use JSON.parse() on this data. I really didn't know what to expect from doing this.
db.any('select * from blog')
.then(function (data) {
data = JSON.parse(data); // attempting to parse the Postgres data
console.log(data);
})
An error occurred:
ERROR: SyntaxError: Unexpected token o in JSON at position 1
I also tried to call on this data as if it were an object.
db.any('select * from blog')
.then(function (data) {
console.log(data);
console.log(data.id); // attempting to get just the id from the data
})
Which output to the terminal as:
undefined
How can I use this data within a js environment like Express? Not sure if it makes a difference, but I'm trying to use Pug to template everything to the front-end.
The select query returns array of objects with each element in the array corresponding to a row in the table (blog) in your case and each key in the object corresponding to the table column.
I am not sure why you are trying to JSON-parse it, as JSON expects an object, and it cannot parse arrays.
Try JSON.parse(JSON.stringify(data));

How to send ISODate in meteor.call method

I am creating an array of objects in client side in the meteor, and each object have modified date inside it as mentioned below:
Client Side:
student;
here student is an array of an object contains name, id, roll_no
var array = [];
student.forEach(function(singleStud, index){
var single_obj ={
"name":singleStud.name,
"student_id":singleStud.id,
"roll_no":singleStud.roll_no,
"college_name":"ABC college",
"college_id":"xyz Id",
"created_date": new Date()
}
array.push(single_obj);
},this)
Meteor.call('saveDetails', array, function (error, result) {
console.log("data Saved Successfully");
});
Server Side:
I have used plugin mikowals:batch-insert to insert an array which is equivalent to insertMany in mongo.
Meteor.methods({
"saveDetails": function (array) {
try {
studentDetails.batchInsert(array);
return true;
} catch (err) {
return err;
}
}
});
when I save it the created_date is saving it as a string ("2018-04-23T10:26:26.766Z"), but I want it to be saved it as a date data type (ISODate("2018-04-23T10:26:26.766Z")).
How can I achieve from the client side in meteor.call ???
This is actually a bug in mikowals:batch-insert. mikowals-batch-insert recursively attempts to convert objects into a JSON format friendly with MongoDB. As part of this process, it uses underscore clone to make shallow copies of primitive types. While Date is a primitive, it cannot be cloned with _.clone, so it mutates your date to a string (eww). You should open an issue with mikowals:batch-insert.
Regardless, you shouldn't be defining this data on the client. The client could maliciously inject false information (which could break your application's logic). Instead, you should map over the input and inject the date into incoming objects.