Details
-
Improvement
-
Resolution: Works as Designed
-
Minor - P4
-
None
-
None
-
None
-
Query Execution
-
QE 2023-06-12
Description
How are you using Mongo? What version of the server and driver are you using?
MongoDB 6.0
MongoDB NodeJS Driver 5.1
What is the feature/improvement you would like?
When documents are upserted, insert them exactly as specified in other words maintain the position of each field.
What use case would this feature/improvement enable?
More pleasant documents that fit the form I want it to as well as requiring less function calls to the server to replicate the desired behavior
Code example:
const task = {
|
kind: 'repeating', |
name: options.name,
|
repeatsEvery: options.repeatsEvery,
|
error: null, |
startsAt: nextRunAt,
|
lastFinishedAt: null, |
lastFailedAt: null, |
lockedAt: null, |
createdAt: now,
|
updatedAt: now
|
};
|
|
|
/*
|
const exists = await _collection.findOne({ name: options.name });
|
if (exists) {
|
// We don't use upsert because we want to maintain the positions of each field
|
await _collection.updateOne({ _id: exists._id }, { $set: task });
|
} else {
|
await _collection.insertOne(task);
|
}
|
*/
|
|
|
await collection.updateOne({ name: options.name }, { $set: task }, { upsert: true }); |
|
|
// Produces:
|
const generated = {
|
_id: {
|
$oid: '63fc0358c071ce978101c877' |
},
|
name: 'add_two_single_tasks', |
createdAt: {
|
$date: {
|
$numberLong: '1677460312226' |
}
|
},
|
error: null, |
kind: 'repeating', |
lastFailedAt: null, |
lastFinishedAt: {
|
$date: {
|
$numberLong: '1677460327252' |
}
|
},
|
lockedAt: null, |
repeatsEvery: 5000,
|
startsAt: null, |
updatedAt: {
|
$date: {
|
$numberLong: '1677460327252' |
}
|
}
|
};
|
|
|
// Instead of:
|
|
|
const desired = {
|
_id: {
|
$oid: '63fc0578591cc70ce3f9665e' |
},
|
kind: 'repeating', |
name: 'add_two_single_tasks', |
repeatsEvery: 5000,
|
error: null, |
startsAt: {
|
$date: {
|
$numberLong: '1677460866287' |
}
|
},
|
lastFinishedAt: {
|
$date: {
|
$numberLong: '1677460861287' |
}
|
},
|
lastFailedAt: null, |
lockedAt: null, |
createdAt: {
|
$date: {
|
$numberLong: '1677460856262' |
}
|
},
|
updatedAt: {
|
$date: {
|
$numberLong: '1677460861287' |
}
|
}
|
};
|
Attachments
Issue Links
- related to
-
SERVER-76705 Fields are reordered alphabetically by key when editing an existing document
-
- Closed
-