mongoDb 中键值的顺序 [英] Order of key values in mongoDb

查看:82
本文介绍了mongoDb 中键值的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongo 代码:

db.temperature.insert({x":3,y":4});

db.temperature.insert({"x":3,"y":4});

db.temperature.find();

db.temperature.find();

输出{ "_id";: ObjectId("52b418fb132c1f3236831447"), "y";: 4, x": 3 }

OUTPUT { "_id" : ObjectId("52b418fb132c1f3236831447"), "y" : 4, "x" : 3 }

请帮助我理解为什么在我的情况下(上图)即使插入顺序不同,find 方法也会先显示 Y 值,然后显示 x 值.

感谢任何帮助.

推荐答案

引用 https://stackoverflow.com/a/6453755/1150636

MongoDB 中的文档结构和集合结构都基于 JSON 原理.JSON 是一组键/值对(特别是文档的 fieldName/fieldValue 和集合的索引/文档).从这个角度来看,你似乎完全不能依赖秩序.

Both document structure and collection structure in MongoDB based on JSON principles. JSON is a set of key/value pairs (in particular fieldName/fieldValue for document and index/document for collection). From this point of view it doesn't seem that you can rely on order at all.

这意味着没有特定的顺序要求 mongodb 返回一条记录的字段.您需要准备好以可能返回的任何顺序解析结果.

This means that there is no particular order that mongodb is required to return the fields of one record. You need to be ready to parse the results in whichever order they may be returned.

这篇关于mongoDb 中键值的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆