-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
@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:
|
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:
|
apologies @fazelmk, this fell off my radar. I'll knock this out today. |
@lineus Just update my mongoose from master and still got the problem. Log of the action please note that weekProcessedData is an object and not an array. |
@fazelmk the fix was in 5.3.9, can you please provide some code samples? |
BUG
What is the current behavior?
What I sent to mongoose:
What mongoose sent to MongoDB
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
The text was updated successfully, but these errors were encountered: