Like what the title said, I'm looking to find the best way to store an array in mongodb (using mongoose) and retrieve the array at anytime and manipulate it?
I tried storing the array as a string in mongoDB and converting it back to an array when I needed to edit it, but I feel that this way is impractical.
So my question is how can I store the array in mongoDB using the "array" Schema Type and what is the best and most effective way to retrive and edit and restore my array?
For example
eg = [0, 1, 2, 3]
I want to edit the array to this:
eg = [0, 1, 6, 3]
Is there a way i could easily access the array?
Although I am not sure exactly what are you trying to accomplish, I will assume that you wish to replace the 3rd value (index=2) in the array (value 2) with the new value 6.
Let's say that schema looks like:
let arrayTestSchema = mongoose.Schema(
{
name: {type: String, required: true},
values: [Number]
}
)
Insert can be done with:
arrayTest.create({name:"test1", values:[0,1,2,3]})
And the replacement with:
arrayTest.updateOne({name:"test1"}, {$set: {"values.2":6}})
Please note that 2 in values.2 represents the index in the array values.
My index has a string field containing a variable length random id. Obviously it shouldn't be analysed.
But I don't know much about elasticsearch especially when I created the index.
Today I tried a lot to filter documents based on the length of id, finally I got this groovy script:
doc['myfield'].values.size()
or
doc['myfield'].value.size()
both returns mysterious numbers, I think that's because of the field got analysed.
If it's really the case, is there any way to get the original length or fix the problem, without rebuild the whole index?
Use _source instead of doc. That's using the source of the document, meaning the initial indexed text:
_source['myfield'].value.size()
If possible, try to re-index the documents to:
use doc[field] on a not-analyzed version of that field
even better, find out the size of the field before you index the document and consider adding its size as a regular field in the document itself
Elasticsearch stores a string as tokenized in the data structure ( Field data cache )where we have script access to.
So assuming that your field is not not_analyzed , doc['field'].values will look like this
"In america" => [ "in" , "america" ]
Hence what you get from doc['field'].values is a array and not a string.
Now the story doesn't change even if you have a single token or have the field as not_analyzed.
"america" => [ "america" ]
Now to see the size of the first token , you can use the following request
{
"script_fields": {
"test1": {
"script": "doc['field'].values[0].size()"
}
}
}
I am running a query to try and get a count of duplicate id records.
"song" is a subdocument which contains just an _id field in the database. After I run the query I am getting these strange characters in my console output, how come I am not able to get the actual id string that looks like, "555699e4ab3e43ec12accaf9"?
Those characters are the 12 byte id binary string.
And it's actually _id.id i.e. id property on the _id object which is an ObjectID.
Your 24 byte binary "555.." is _id itself, which converts to string automatically if you use it as one.
* more details on how object can be represented as a string: Object.prototype.toString()
Unfortunately for you, whatever IDE you're using is showing it as an object (which is actually what it really is).
Try console.log(util.inspect(results)) (be sure to require the util module)
In any case, console.log(result[0]._id) will give you the 24 byte hex string "5556...ccaf9".
If you don't want the _id which is an object, there's also a getter id which is a String by default.
console.log(typeof result[0]._id) //=> "object"
console.log(typeof result[0].id) //=> "string"
I'm building a web application over Node.js and MongoDB which is based on geolocated points.
The document is something like this:
{ name: ""
keywords: [Array of strings]
location: {lng: double, lat: double }
}
I am wondering how could I use find() to find documents that are near from a coordinate but, in addition, are coincident with any of he keywords in the keywords array.
Imagine that keywords are: ["restaurant", "bar", "coffee"]
I've looked into 2d Index, but the secondary index must be a string. It can't be an array of strings.
The problem is that a document could have more than one keyword (or category) so I can't use a simple string to query them
How would you implement this?
Thanks!
What version of mongo? It looks like this was added in 2.4.0: SERVER-8457
I'm using mongoose schemas for node.js along with express-validator (which has node-validator santiziations and validators).
What's a good way to store price for an item?
I currently have
var ItemSchema = new Schema({
name : { type: String, required: true, trim: true }
, price : Number
});
Price is optional, so I have:
if ( req.body.price ) {
req.sanitize('price').toFloat();
req.assert('price', 'Enter a price (number only)').isFloat();
}
express-validator gives me isNumeric (allows 0 padding), isDecimal, and isInt...I'd rather just convert to decimal and strip all characters, so I'm always inserting 42.00 into db.
I want to allow them to enter $42.00, $42, 42, 42.00 and just store 42.00. How can I accomplish this? and still validate that I'm seeing something resembling a number, for example if they enter 'abc' I want to throw an error back to the form using req.assert.
Also, I suppose currency will eventually become an issue...
Update, I found this post which says to store price as integer in cents, so 4200
https://dba.stackexchange.com/questions/15729/storing-prices-in-sqlite-what-data-type-to-use
I just need a way to convert 4200 to $42.00 when I call item.price and also sanitize and convert the input into 4200.
This is what I ended up doing...
I stored price as cents in database, so it is 4999 for 49.99 as described here: https://dba.stackexchange.com/questions/15729/storing-prices-in-sqlite-what-data-type-to-use
the getPrice will convert it back to readable format, so I can use item.price in my views w/o modifying it.
the setPrice converts it to cents.
model:
var ItemSchema = new Schema({
name : { type: String, required: true, trim: true }
, price : {type: Number, get: getPrice, set: setPrice }
});
function getPrice(num){
return (num/100).toFixed(2);
}
function setPrice(num){
return num*100;
}
I opted to only allow digits and decimal in price field, without $.
So they can enter 49, 49.99, 49.00, but not 49.0 or $49
validation using regex:
if ( req.body.price ) {
req.assert('price', 'Enter a price (numbers only)').regex(/^\d+(\.\d{2})?$/);
}
I wish there was a way to allow the $ because I think its a usability issue, just let the user enter it, but strip it off. I'm not sure how to do that and still validate that we have a price and not a bunch of letters for example.
Hint: The method described here is basically just another implementation of chovy's answer.
Workaround for Mongoose 3 & 4:
If you have trouble to define getters and setters directly in the schema, you could also use the schema.path() function to make this work:
var ItemSchema = new Schema({
name: String,
price: Number
});
// Getter
ItemSchema.path('price').get(function(num) {
return (num / 100).toFixed(2);
});
// Setter
ItemSchema.path('price').set(function(num) {
return num * 100;
});
A bit late but...
The answer of chovy almost worked for me – I just needed to add
{ toJSON: { getters: true }} as an options parameter in the schema declaration.
Example:
import mongoose from 'mongoose'
const productosSchema = new mongoose.Schema(
{
name: String,
price: {
type: Number,
get: v => (v/100).toFixed(2),
set: v => v*100
}
},
{
toJSON: { getters: true } //this right here
}
);
export default mongoose.model('productos', productosSchema)
This works on Mongoose 6.0.14.
References: https://mongoosejs.com/docs/api.html#document_Document-toJSON
Adds schema type "Currency" to mongoose for handling money. Strips out common characters automatically (",", "$" and alphabet chars)
https://github.com/paulcsmith/mongoose-currency
What it does:
Saves a String as an integer (by stripping non digits and multiplying by 100) to prevent rounding errors when performing calculations (See gotchas for details)
Strips out symbols from the beginning of strings (sometimes users include the currency symbol)
Strips out commas (sometimes users add in commas or copy paste values into forms, e.g. "1,000.50)
Only save from two digits past the decimal point ("$500.559" is converted to 50055 and doesn't round)
Strips [a-zA-Z] from strings
Pass in a string or a number. Numbers will be stored AS IS.
Assumes that if you set the value to an integer you have already done the conversion (e.g. 50000 = $500.00)
If a floating point number is passed in it will round it. (500.55 -> 501). Just pass in integers to be safe.
Hope it helps some1.
I've been researching for a while on this topic, because I want to store not only price, but version, which both may have trailing 0s that get chopped off when stored as a number. As far as I know, Mongoose/MongoDB can't save a number with trailing zeroes.
Unless you save the number as a string.
Aside from storing numbers in tens or thousands and dividing or parsing, you can also store it as a string. This means, you can always just print it out when you need to show "1.0" or "1.00" by just using the variable without any conversion. Due to JavaScript being untyped, you can still compare it to numbers (make sure it's on the left hand side). Var < 10, for example, will return the right evaluation, even when var is a string. If you're comparing two variables, you'd need to make sure that they're both numbers, though. When you need a number, you can multiply the string by one (var * 1 < var2 * 1), which will ensure that JavaScript treats the var as a number, although it will lose the trailing zeros.
On the one hand, storing it as a string means you need to do a conversion every time you want to use the variable as a number. On the other hand, you would presumably be doing a numeric conversion anyway (var / 100) every time you want to use a cents number as a dollar amount. This option would depend on how frequently you need to your value as a number. Also it may cause bigger bugs if you forget that your variable is a string than if you forget that your variable is in cents.
(However, it's a great fit for version numbers that would only ever be used for display and comparison.)
The numeral module will accomplish that:
http://numeraljs.com/
https://www.npmjs.com/package/numeral