Decimal support Mongodb - node.js

Trying to use decimal128 datatype in my nodejs application.
mongo version "3.4.2",
os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "16.7.0"
nodejs version 6.11.2
mongoose version 4.11.13
mongoose using native mongodb driver version 2.2.31
Mongodb config:
storage:
engine: wiredTiger
dbPath: "/Users/backend/Desktop/mongo/data"
What am i doing?
I've got mongoose subdocument schema
const Premises = mongoose.Schema({
floor : { type: mongoose.Schema.Types.Decimal128, required: true },
deleted : { type: Boolean, default: false },
created_at : { type: Date, default: Date.now },
updated_at : { type: Date, default: Date.now }
});
This schema is a subdocument of document with schema below:
...
premises : [ Premises ],
...
To add new subdocuments im using update method:
var queryFilter = {
'deleted' : false,
'buildings._id' : params.building_id
};
var premise = {
'_id': mongoose.Types.ObjectId(),
'floor': params['floor']
};
Block.update(queryFilter, { '$addToSet': { 'buildings.$.premises': premise } }, { safe: true }, function (error, result) {
result['_id'] = premise['_id'];
callback(error, result || null);
return;
});
Also i used code below:
var queryFilter = {
'deleted' : false,
'buildings._id' : params.building_id
};
var premise = {
'_id': mongoose.Types.ObjectId(),
'floor': mongoose.Types.Decimal128.fromString(params['floor'])
};
Block.update(queryFilter, { '$addToSet': { 'buildings.$.premises': premise } }, { safe: true }, function (error, result) {
result['_id'] = premise['_id'];
callback(error, result || null);
return;
});
But get the same error in both situations:
{"errors":{"name":"MongoError","message":"$numberDecimal is not valid
for
storage.","driver":true,"index":0,"code":52,"errmsg":"$numberDecimal
is not valid for
storage."},"data":{"ok":0,"n":0,"nModified":0,"_id":"59ce4e8cecba947a9a342f37"}}
I dont wanna use some workaround like
mongoose-double
to support negative numbers in my collections.
Much thanx for ur answers and solutions.

I faced with the same issue "$numberDecimal is not valid for storage" when I tried to add subdocument into array.
I tend to think this happens because of
Decimal128.prototype.toJSON = function() {
return { "$numberDecimal": this.toString() };
}
from http://mongodb.github.io/node-mongodb-native/2.2/api/node_modules_bson_lib_bson_decimal128.js.html
Maybe that is not the best solution but workaround below helped me:
mongoose.Types.Decimal128.prototype.toJSON = mongoose.Types.Decimal128.prototype.toString;
For adding item to an existing array I used:
entity.subentities.addToSet(subentity);
const updatedEntity = await entity.save();

Related

Mongoose reports no error on updating, but does not update

losing my mind here for something for a MongoDB document update with Mongoose, not reporting any error but not actually updating successfully.
I have this schema:
/**
* Branch Schema
*/
let BranchSchema = new Schema({
name: String,
domain: String,
email: String,
bm: { type: Schema.ObjectId, ref: 'User' },
st: [{ type: Schema.ObjectId, ref: 'User' }],
stockCurrent: {
paper: Schema.Types.Object,
ink: Schema.Types.Object
},
stockNeeded: {
paper: Schema.Types.Object,
ink: Schema.Types.Object
},
}, { versionKey: false, usePushEach: true });
mongoose.model('Branch', BranchSchema);
Trying to update stockCurrent, using this logic:
Branch.findById(config.branch.id, function (err, branch) {
if (err) {
res.status(422).send({
message: 'הסניף לא נמצא'
});
} else {
console.log(branch);
Object.keys(req.body.stock).forEach(function (type) {
Object.keys(req.body.stock[type]).forEach(function (code) {
if (req.body.stock[type][code] > 0) {
if (typeof branch.stockCurrent[type][code] === 'undefined') {
branch.stockCurrent[type][code] = 0;
}
branch.stockCurrent[type][code] += req.body.stock[type][code];
}
});
});
console.log(branch);
branch.save(function (err, updated) {
console.log("err: " + err);
if (err) {
stock.remove();
res.status(422).send({
message: 'שגיאה בשמירת מלאי'
});
} else {
console.log(updated);
res.send({
message: 'מלאי נוסף בהצלחה'
});
}
});
}
});
I get to to success part, having my console log this:
{
"_id":5dd276a6bcc29a13789fcecb,
"name":"בצלאל ארכיטקטורה",
"domain":"bezalel.eazix.io",
"email":"eazix.1.bezalel#gmail.com",
"bm":5cdd2130d192ea03a87d2dfd,
"stockNeeded":{
"ink":{
"GY":2,
"PM":2,
"M":2,
"MBK":2,
"PBK":2,
"PC":2,
"Y":2,
"C":2,
"waste":2
},
"paper":{
"COATED":5,
"PLAIN":5,
"PHOTO":3
}
},
"stockCurrent":{
"paper":{
"PLAIN":0
},
"ink":{
"waste":0
}
},
"st":[
]
}{
"_id":5dd276a6bcc29a13789fcecb,
"name":"בצלאל ארכיטקטורה",
"domain":"bezalel.eazix.io",
"email":"eazix.1.bezalel#gmail.com",
"bm":5cdd2130d192ea03a87d2dfd,
"stockNeeded":{
"ink":{
"GY":2,
"PM":2,
"M":2,
"MBK":2,
"PBK":2,
"PC":2,
"Y":2,
"C":2,
"waste":2
},
"paper":{
"COATED":5,
"PLAIN":5,
"PHOTO":3
}
},
"stockCurrent":{
"paper":{
"COATED":1,
"PHOTO":2,
"PLAIN":0
},
"ink":{
"PM":1,
"waste":0
}
},
"st":[
]
}**"err":null**{
"_id":5dd276a6bcc29a13789fcecb,
"name":"בצלאל ארכיטקטורה",
"domain":"bezalel.eazix.io",
"email":"eazix.1.bezalel#gmail.com",
"bm":5cdd2130d192ea03a87d2dfd,
"stockNeeded":{
"ink":{
"GY":2,
"PM":2,
"M":2,
"MBK":2,
"PBK":2,
"PC":2,
"Y":2,
"C":2,
"waste":2
},
"paper":{
"COATED":5,
"PLAIN":5,
"PHOTO":3
}
},
"stockCurrent":{
"paper":{
"COATED":1,
"PHOTO":2,
"PLAIN":0
},
"ink":{
"PM":1,
"waste":0
}
},
"st":[
]
}
I can see the here the initial state, the updated version before saving, and the the err:null, and the allegedly updated document.
but alas! the document wasn't really updated. it remains the same.
I have tried many things, searching and looking for similar cases, checking my schema, adding useStrict:false to the schema, nothing helps.
Mongoose ver 4.13.20, Mongodb ver 3.6.17
SOS
Dor
I'm guessing the SchemaTypes are the problem? In Mongoose 4.x, these are the only valid SchemaTypes:
String
Number
Date
Buffer
Boolean
Mixed
Objectid
Array
Notice that Mixed is an option but not Object. You need to tell Mongoose that you updated a Mixed field using model.markModified('pathName'). See the Mixed docs.
So in your case, the code below may fix the issue:
branch.markModified('stockCurrent');
branch.save(function (err, updated) {
// ...

Mongoose : update a document in a pre save hook

i'm quite new to nodeJS and have some problem i cannot solve...
i would like to update a related document in a pre save hook in mongoose :
No errors, but as a result i always get:
updated:{"n":1,"nModified":0,"ok":1}
so document is never updated.... how can i solve this ? i know i can make all controls before, but pre-hook seems the good place for that.
thanks for your help...
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var suivilotSchema = new Schema({
code_lot: { type: String,Required: 'Code Lot cannot be left blank.' },
site_IMS: { type: String,Required: 'Site IMS cannot be left blank'},
date_plantws: { type: String,Required: 'tws plan prod cannot be left blank'},
nb_server: { type: Number,Required: 'nb Server cannot be left blank.',default: 1},
nb_server_done: { type: Number,Required: 'nb Server done cannot be left blank.',default: 0},
date_created: { type: Date,default: Date.now },
date_updated: { type: Date },
retry: { type: Number },
status: { type: Number}
});
suivilotSchema.pre('save', function (next) {
console.log('***** PRE HOOK suivilot *******');
var self = this;
this.constructor.findOne({'code_lot' : self.code_lot,'date_plantws' : self.date_plantws, 'retry' :
self.retry },function (err,existinglot) {
if (!existinglot){
console.log('SUIVILOTS : pas de resultat');
next();
}
else{
console.log('SUIVILOTS : lot exists: ',existinglot._id);
existinglot.updateOne({ '_id' : existinglot._id }, { $inc:{'nb_server' : 1}
},function(err,updated){
if(err)
console.log("Error during increment server :"+err);
console.log('updated:'+JSON.stringify(updated));
console.log('updated:'+JSON.stringify(existinglot));
});
}
});
});

How to grab field value during a MongooseModel.bulkWrite operation?

Context:
I am trying to upsert in bulk an array of data, with an additional computed field: 'status'.
Status should be either :
- 'New' for newly inserted docs;
- 'Removed' for docs present in DB, but inexistent in incoming dataset;
- a percentage explaining the evolution for the field price, comparing the value in DB to the one in incoming dataset.
Implementations:
data.model.ts
import { Document, model, Model, models, Schema } from 'mongoose';
import { IPertinentData } from './site.model';
const dataSchema: Schema = new Schema({
sourceId: { type: String, required: true },
name: { type: String, required: true },
price: { type: Number, required: true },
reference: { type: String, required: true },
lastModified: { type: Date, required: true },
status: { type: Schema.Types.Mixed, required: true }
});
export interface IData extends IPertinentData, Document {}
export const Data: Model<IData> = models.Data || model<IData>('Data', dataSchema);
data.service.ts
import { Data, IPertinentData } from '../models';
export class DataService {
static async test() {
// await Data.deleteMany({});
const data = [
{
sourceId: 'Y',
reference: `y0`,
name: 'y0',
price: 30
},
{
sourceId: 'Y',
reference: 'y1',
name: 'y1',
price: 30
}
];
return Data.bulkWrite(
data.map(function(d) {
let status = '';
// #ts-ignore
console.log('price', this);
// #ts-ignore
if (!this.price) status = 'New';
// #ts-ignore
else if (this.price !== d.price) {
// #ts-ignore
status = (d.price - this.price) / this.price;
}
return {
updateOne: {
filter: { sourceId: d.sourceId, reference: d.reference },
update: {
$set: {
// Set percentage value when current price is greater/lower than new price
// Set status to nothing when new and current prices match
status,
name: d.name,
price: d.price
},
$currentDate: {
lastModified: true
}
},
upsert: true
}
};
}
)
);
}
}
... then in my backend controller, i just call it with some route :
try {
const results = await DataService.test();
return new HttpResponseOK(results);
} catch (error) {
return new HttpResponseInternalServerError(error);
}
Problem:
I've tried lot of implementation syntaxes, but all failed either because of type casting, and unsupported syntax like the $ symbol, and restrictions due to the aggregation...
I feel like the above solution might be closest to a working scenario but i'm missing a way to grab the value of the price field BEFORE the actual computation of status and the replacement with updated value.
Here the value of this is undefined while it is supposed to point to current document.
Questions:
Am i using correct Mongoose way for a bulk update ?
if yes, how to get the field value ?
Environment:
NodeJS 13.x
Mongoose 5.8.1
MongoDB 4.2.1
EUREKA !
Finally found a working syntax, pfeeeew...
...
return Data.bulkWrite(
data.map(d => ({
updateOne: {
filter: { sourceId: d.sourceId, reference: d.reference },
update: [
{
$set: {
lastModified: Date.now(),
name: d.name,
status: {
$switch: {
branches: [
// Set status to 'New' for newly inserted docs
{
case: { $eq: [{ $type: '$price' }, 'missing'] },
then: 'New'
},
// Set percentage value when current price is greater/lower than new price
{
case: { $ne: ['$price', d.price] },
then: {
$divide: [{ $subtract: [d.price, '$price'] }, '$price']
}
}
],
// Set status to nothing when new and current prices match
default: ''
}
}
}
},
{
$set: { price: d.price }
}
],
upsert: true
}
}))
);
...
Explanations:
Several problems were blocking me :
the '$field_value_to_check' instead of this.field with undefined 'this' ...
the syntax with $ symbol seems to work only within an aggregation update, using update: [] even if there is only one single $set inside ...
the first condition used for the inserted docs in the upsert process needs to check for the existence of the field price. Only the syntax with BSON $type worked...
Hope it helps other devs in same scenario.

Mongoose async requests managment

I'm actually trying to convert mongodb references into those references' documents value (info.value) using mongoose in javascript.
Tried that by using map, for/forEach, and nothing did the job since mongoose requests are async.
Not really used to this kind of code, I feel a bit lost after all those things I tried.
Maybe someone would like to give me a hint about this by taking a look at the code below.
Just for information, no need to worry about loading templates, connecting to mongo, ... since everything else is working just fine.
That's the closest I got to the expected result, but still, it throws me errors when I try to "console.log(cond[c]);/console.log(info);" (cond[c] and info are null and undefined)
Well this function also needs to be prepared to be recursive since I plan to put sub-blocks in the "content" property of the bloc objects.
Thanks a lot for your time guys.
// Input condition
"H1Vf3KTef || false"
// Expected result
"1 || false"
// Buggy Function
var execIfBlock = function recursExec (query, callback) {
IfBlockModel.findOne(query, function(err, ifBlock) {
if (!err) {
var cond = ifBlock.condition.split(" ");
//console.log('Block : ' + ifBlock);
//console.log('Condition : ' + cond);
var calls = new Array();
for (var c = 0, len = cond.length; c < len; c++) {
if (shortId.isValid(cond[c])) {
calls.push(function() {
InfoModel.findOne({ _id: cond[c] }, function(err, info) {
console.log(cond[c]);
console.log(info);
cond[c] = info.value;
});
});
}
}
async.parallel(calls, function(err, result) {
console.log(result);
// Do some job using the final expected result : "1 || false"
});
}
});
};
// Info template
{
"_id": "H1Vf3KTef",
"value": "1"
}
// Bloc template
{
"_id": "rkRBtLTef",
"content": [],
"condition": "H1Vf3KTef || false"
}
// Info schema
var InfoSchema = new Schema({
_id: { type: String, unique: true, required: true, default: shortId.generate },
value: { type: String, default: "0" }
});
// Bloc schema
var IfBlockSchema = new Schema({
_id: { type: String, unique: true, required: true, default: shortId.generate },
condition: { type: String, required: true, default: true },
content: [{ type: String, required: true, default: '', ref: 'block' }]
});
Use promises and break your code in small functions :
var execIfBlock = function recursExec(query, callback) {
IfBlockModel.findOne(query, function (err, ifBlock) {
if (!err) {
var cond = ifBlock.condition.split(" ");
updateMultipeInfo(cond)
.then(values => {
console.log(values) // [values1, values ,...]
});
}
});
};
function updateMultipeInfo(cond){
return Promise.all(cond.map(updateInfo))
}
function updateInfo(id){
if (shortId.isValid(id)) {
return InfoModel
.findOne({ _id: id })
.then(info => info.value);
} else {
return Promise.reject("invalid id");
}
}

Mongodb $set not working

I'm trying to update the nested document. The query is returning the correct document, but the property isn't being updated.
Model:
{
_id: '560b072434b72aa4050fff9f',
trips: [
{
tripId: '561581ef9387780e76469e96',
startDate: "2015-11-17T06:00:00.000Z",
endDate: "2015-11-18T06:00:00.000Z"
},{
tripId: '5617d1bb1d42c4da90d3bdea',
startDate: "2015-10-17T06:00:00.000Z",
endDate: "2015-10-18T06:00:00.000Z"
}
],
}
Query:
UserData.update(
{ '_id': req.query._id, 'trips.tripId': req.query.tripId },
{ '$set': { 'trips.$.startDate' : req.query.newStartDate,
'trips.$.endDate' : req.query.newEndDate} },
{ 'multi': true },
function(e, doc){
console.log(doc);
}
);
Schema:
var userDataSchema = {
name: String,
trips: Array
};
#BlakesSeven led me to the answer. I added:
var ObjectId = require('mongodb').ObjectID;
before my app.route in server.js, installed mongodb using npm, then changed
{ '_id': req.query._id, 'trips.tripId': req.query.tripId },
to
{ '_id': ObjectId(req.query._id), 'trips.tripId': ObjectId(req.query.tripId) },.
Everything updates as it should now. Thanks Blakes Seven!

Resources