Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

BulkWrite updateOne Converting number to array #7098

Closed
fazelmk opened this issue Oct 6, 2018 · 5 comments
Closed

BulkWrite updateOne Converting number to array #7098

fazelmk opened this issue Oct 6, 2018 · 5 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@fazelmk
Copy link

fazelmk commented Oct 6, 2018

BUG

What is the current behavior?
What I sent to mongoose:

[ { updateOne:{
    "filter":{"_id":"5bb76e2c1fd572394cfb31d4"},
     "upsert": false,
     "update":{ "weekProcessedData.3.0":23.0000000001485}
  },
  { updateOne: {
      "filter":{"_id":"5bb76e2c1fd572394cfb31df"},
      "upsert":false,
      "update":{"weekProcessedData.3.0":22.500000000145267}
   },
   {updateOne: {
     "filter":{"_id":"5bb76e2c1fd572394cfb31ec"},
     "upsert":false,
     "update":{"weekProcessedData.3.0":22.500000000145267}
   }
]

What mongoose sent to MongoDB

Mongoose: pointdatas.bulkWrite([ { updateOne: { filter: { _id: 5bb76e2c1fd572394cfb31d4 }, update: { '$set': { 'weekProcessedData.3.0': [ 23.0000000001485 ] } }, upsert: false } }, { updateOne: { filter: { _id: 5bb76e2c1fd572394cfb31df }, update: { '$set': { 'weekProcessedData.3.0': [ 22.500000000145267 ] } }, upsert: false } }, { updateOne: { filter: { _id: 5bb76e2c1fd572394cfb31ec }, update: { '$set': { 'weekProcessedData.3.0': [ 22.500000000145267 ] } }, upsert: false } } ], { ordered: false })

The set value should be a number not an array containing the number.
in the Schema weekProcessedData is an object with 7 fixed keys 1..7 and each key contains an array of 1440 positions.

the update should set a value for the specified position ( in this case the position 0 ).
But instead it converts the value to an array containing the value and save it. Tested in the mongo shell works perfectly.

What is the expected behavior?
It's expected to send to the mongoDB whatever I put in the value and no to convert it to an array on it's own.

Please mention your node.js, mongoose and MongoDB version.
Node.js v6.9.5
mongoose: 5.3.1
MongoDB 3.6

@ghost
Copy link

ghost commented Oct 6, 2018

@fazelmk I tried this update syntax with bulkWrite, findOneAndUpdate, and then eventually with the native driver directly. It fails in both of the mongoose operations as described, and succeeds in the native driver. Thanks for reporting this, I'll see if I can sort this one out 👍

7098.js

#!/usr/bin/env node
'use strict';

const assert = require('assert');
const mongoose = require('mongoose');
const { Schema, connection} = mongoose;
const DB = '7098';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { family: 4, useNewUrlParser: true };

const schema = new Schema({
  xyz: [[Number]]
});

const Test = mongoose.model('test', schema);

const test = new Test({
  xyz: [
    [ 0, 1 ],
    [ 2, 3 ],
    [ 4, 5 ]
  ]
});

const bulkObject = [{
  updateOne: {
    filter: { _id: test._id },
    upsert: false,
    update: { $set: { 'xyz.1.0': 200 } }
  }
}];


async function run() {
  assert.strictEqual(mongoose.version, '5.3.1');
  await mongoose.connect(URI, OPTS);
  await connection.dropDatabase();
  const inserted = await Test.create(test);
  assert.deepStrictEqual(inserted.toObject().xyz, [[0, 1], [2, 3], [4, 5]]);
  await Test.bulkWrite(bulkObject);
  const updated = await Test.findOne({});
  assert.deepStrictEqual(updated.toObject().xyz, [[0, 1], [200, 3], [4, 5]]);
  console.log('All Assertions Pass.');
  await connection.close();
}

run().catch(error);

function error(e) {
  console.error(e);
  return connection.close();
}

Output:

issues: ./7098.js
{ AssertionError [ERR_ASSERTION]: Input A expected to strictly deep-equal input B:
+ expected - actual

- []
+ [
+   [
+     0,
+     1
+   ],
+   [
+     200,
+     3
+   ],
+   [
+     4,
+     5
+   ]
+ ]
    at run (/Users/lineus/dev/node/mongoose/issues/7098.js:42:10)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  generatedMessage: true,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: [],
  expected: [ [ 0, 1 ], [ 200, 3 ], [ 4, 5 ] ],
  operator: 'deepStrictEqual' }
issues:

@ghost ghost added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 6, 2018
@ghost
Copy link

ghost commented Oct 6, 2018

for grins, here's the native script:

7098_native.js

#!/usr/bin/env node
'use strict';

const assert = require('assert');
const { MongoClient } = require('mongodb');
const URL = 'mongodb://localhost:27017/gh7098';
const OPTS = { useNewUrlParser: true };

async function run() {
  let mc = new MongoClient(URL, OPTS);
  let conn = await mc.connect();
  let db = conn.db('gh7098');
  await db.dropDatabase();
  let collection = db.collection('tests');
  await collection.insertOne({
    xyz: [
      [0, 1],
      [2, 3],
      [4, 5]
    ]
  });
  let inserted = await collection.findOne({});
  assert.deepStrictEqual(inserted.xyz, [[0, 1], [2, 3], [4, 5]]);
  let cond = {};
  let update = { $set: { 'xyz.1.0': 200 } };
  let opts = { new: true };
  await collection.findOneAndUpdate(cond, update, opts);
  let updated = await collection.findOne({});
  assert.deepStrictEqual(updated.xyz, [[0, 1], [200, 3], [4, 5]]);
  
  console.log('All Assertions Pass.');
  return conn.close();
}

run().catch(error);

function error(e) {
  console.error(e);
  process.exit(1);
}

Output:

issues: ./7098_native.js
All Assertions Pass.
issues:

@ghost
Copy link

ghost commented Nov 1, 2018

apologies @fazelmk, this fell off my radar. I'll knock this out today.

@fazelmk
Copy link
Author

fazelmk commented Nov 7, 2018

@lineus Just update my mongoose from master and still got the problem.

Log of the action
Studio3T query

please note that weekProcessedData is an object and not an array.

@vkarpov15
Copy link
Collaborator

@fazelmk the fix was in 5.3.9, can you please provide some code samples?

@vkarpov15 vkarpov15 added this to the 5.3.9 milestone Nov 10, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants