Lazy
Lazy

Reputation: 277

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?

Upvotes: 0

Views: 1225

Answers (1)

Gaurav Dave
Gaurav Dave

Reputation: 7474

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)

Upvotes: 1

Related Questions