【发布时间】:2019-08-24 09:23:52
【问题描述】:
所以我对 Mongo .create 和 .findAndUpdate 操作如何工作的理解有疑问。我有 mongoose 5.4.2X 和一个带有模式的模型,它有很多 key:value 对(没有任何嵌套对象)按确切顺序(在下面的代码中,我使用 1. 2. 3. 等向您展示正确的顺序)像这样:
let schema = new mongoose.Schema({
1.code: {
type: String,
required: true
},
2.realm: {
type: String,
required: true,
},
3.type: {
type: String,
required: true,
enum: ['D', 'M'],
},
4.open: Number,
5.open_size: Number,
6.key: typeof value,..
7...another one key: value like previous one,
8.VaR_size: Number,
9.date: {
type: Date,
default: Date.now,
required: true
}
});
和一个class 对象,它们具有完全相同的属性以相同的顺序,就像上面的架构一样。
当我通过 const contract = new Class_name (data) 并使用 console.log(contract) 为 Mongo 生成数据时,我有一个必要的对象,其属性的属性正确的顺序,例如:
Contract {1.code: XXX, 2.realm: YYY, 3.type: D, .... 8.VaR_size: 12, 9.date: 327438}
但是当我尝试通过findOneAndUpdate 或 (findByID) 创建/更新文档到数据库时,它会按字母顺序写入,但不是必要的 1->9,例如:
_id:5ca3ed3f4a547d4e88ee55ec
1.code:"RVBD-02.J"
7.VaR:0.9
(not the 1->9)...:...
8.VaR_size:0.22
__v:0
5.avg:169921
sn-p编写的完整代码为:
let test = await contracts.findOneAndUpdate(
{
code: `${item_ticker}-${moment().subtract(1, 'day').format('DD.MMM')}` //how to find
},
contract, //document for writinng and options below
{
upsert : true,
new: true,
setDefaultsOnInsert: true,
runValidators: true,
lean: true
}
).exec();
是的,我已阅读 mongoose 文档,但没有找到任何选项参数 解决我的问题或者可能一些可选参数很重要,但是 没有对此的描述。
这不是我的第一个项目,有趣的是,当我插入 根据架构插入通过
.insertMany的大量文档 顺序(不按字母顺序)
我唯一的问题是:
我该如何解决?是否有任何选项参数或者它是
findAnd....的一部分 操作?如果没有解决方案,如果对我来说,我该怎么办 之前需要正确的排序和检查文件的存在 插入它?
一段时间后更新#1,我改写我的谷歌搜索查询,并在此处找到关于 SW 的相关问题:MongoDB field order and document position change after update
【问题讨论】:
标签: javascript node.js mongodb mongoose mongoose-schema