How to format a number 123456 to 123,456 in mongodb - mongodb

I am getting a number (e.g., 450000) as a result from a mongodb query. I want to format the number with a thousands separator (like currency) as 450,000.
Number: 1593324
Expected Output: 1,593,324
How can I achieve this in mongodb?

you can't achieve this in mongodb because this is not programming language.
But You can use:
variableName.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
and then call to store this string in database
example:
var test = 1593324;
test.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
and then output i m getting :
1,593,324

Related

Laravel 5.0, using where like with integer not work and not much sense with string

I've try to use where and like conditional on phone number or digital number actually this query will retrieved all the rows in my tables even those field is empty.
$cont = ClientCbcContact::where('contact_number_number', 'like', '%'.Request::input('phone').'%')->get();
However I use with string as below to retrieve my family_name which contain data heng sopheak, heng2 sopheak, heng, heng sopheak4 ** and **Request::input('name')= equal to heng sopheak. it select only one rows name heng sopheak.
ClientCbcGeneral::where('family_name', 'like', '%'. Request::input('name') .'%')->get();
MySQL LIKE queries require a string, not an integer, you could try casting the number to a string before performing the query.

Parsing strings in a column with Entity Framework

Our data model in one of the rows looks like:
productName.productDescriptor.productMetadata
There is not a lot of data in this table. Is there a "good" way to parse that data, like get all the
Products where productDescriptor equals <MyDescriptor>
?
You can use a query like this:
var result = myRows.Where(x => x.myColumn.Contains("." + productDescriptor + "."));
If the components of your strings contain the separator period, this will not work.

Why mongodb stores some numbers as NumberLong?

Mongodb is installed on Windows 8 and I use spring-data-mongodb to work with it.
I have collection with field pId, it's a number.
I see strange situation, mongodb stores some pId as simple number but some of them as NumberLong.
query:
db.mycollection.distinct("pId", {"dayDate" : { "$gte" : ISODate("2015-04-14T00:00:00.000Z")}})
output:
[ 61885, 61886, NumberLong(61887) ]
Why it happens and may I change something to use the same data type for all pId values?
I have been using Laravel with MongoDB, and as far as my understanding of MongoDB, I founded that:
If you're saving your data (numbers) in quote than, it will save as string, but if you assign number to variable in normal way (no quotes) then it will save in NumberLong format or when you are doing type cast by using (int)
Example.
$data = '1' (Stores as string)
$data = 1 or $data = (int)'1' (Store as NumberLong)

concatenating text to a column in pig

I have a day column and a month column and would like to concatenate the year to it and store it in CHARARRAY format with the hyphens.
so I have: month:CHARARRAY, day:CHARARRAY
Meaning, for example, if the day column contains '03' and the month column contains '04', I would like to create a date column that contains: '2014-04-03'
This is my code:
CONCAT('2014-',month,'-',day) as date;
It doesn't work and I'm not quite sure how to concatenate additional text onto the column.
I would like to note that I'm not sure converting to date format is an option for me. I would prefer to keep it in CHARARRAY format since I would like to join with another file that has date stored in CHARARRAY format.
Assuming this is the data file called dateExample.csv:
Surender,02,03,1988
Raja,12,09,1998
Raj,05,10,1986
This is the script for pig:
A = LOAD 'dateExample.csv' USING PigStorage(',') AS(name:chararray,day:chararray,month:long,year:chararray);
X = FOREACH A GENERATE CONCAT((chararray)day,CONCAT('-',CONCAT((chararray)month,CONCAT('-',(chararray)year))));
dump X;
You will get the desired output:
(02-3-1988)
(12-9-1998)
(05-10-1986)
Explanation:
When we try to concat like this:
X = FOREACH A GENERATE CONCAT(day,CONCAT('-',CONCAT(month,CONCAT('-',year))));
We get following exception :
ERROR 1045:
<line 2, column 45> Could not infer the matching function for org.apache.pig.builtin.CONCAT as multiple or none of them fit. Please use an explicit cast.
So we need to explicitly cast the day,month and year values to chararray and it works!!

Querying CouchDB documents between a start date and an end date

I've been trying to figure out how to create a CouchDB view that will let me query all the documents that have a start date greater than A and an end date less than B.
Is this possible in CouchDB or another noSQL document store? Should I scrap it and go back to SQL?
I'm simply trying to do the SQL equivalent of:
SELECT * WHERE [start timestamp] >= doc.start AND [end timestamp] < doc.end;
Just create a map like this:
function (doc) {emit(doc.timestamp, 1)}
then query the view with:
?descending=true&limit=10&include_docs=true // Get the latest 10 documents
The view will be sorted oldest to latest so descending=true reverses that order.
If you want a specific range.
?startkey="1970-01-01T00:00:00Z"&endkey="1971-01-01T00:00:00Z"
would get you everything in 1970.
These should help:
http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
http://wiki.apache.org/couchdb/HttpViewApi
http://wiki.apache.org/couchdb/View_collation
Use an array key in your map function
function (doc) {
var key = [doc.start, doc.end]
emit(key, doc)
}
Then to get documents with a start date greater then 1970-01-01T00:00:00Z and an end date before 1971-01-01T00:00:00Z use the query
?startkey=["1970-01-01T00:00:00Z", ""]&endkey=["\ufff0", "1971-01-01T00:00:00Z"]
I was looking for the same thing and stumbled upon this question. With CouchDB 2.0 or higher you have the possibility of using Mango Queries, which includes greater-than and less-than.
A mango query could look like:
"selector": {
"effectiveDate": {
"$gte": "2000-04-29T00:00:00.000Z",
"$lt": "2020-05-01T00:00:00.000Z"
}
}
Use startkey and endkey. This way you can decide your date range at runtime without slowing down your query.