修改属性在MongoDB中的显示顺序 [英] Modify the order in which properties are displayed in MongoDB
问题描述
我正在使用PyMongo将数据(title, description, phone_number ...)
插入到MongoDB中.但是,当我使用mongo
客户端查看数据时,它以奇怪的顺序显示属性.具体来说,首先显示phone_number
属性,然后显示title
,然后显示description
.有什么方法可以强制执行特定命令?
I am using PyMongo to insert data (title, description, phone_number ...)
into MongoDB. However, when I use mongo
client to view the data, it displays the properties in a strange order. Specifically, phone_number
property is displayed first, followed by title
and then comes description
. Is there some way I can force a particular order?
推荐答案
原始答案(2013):
MongoDB文档是BSON对象,是键-值对的无序字典.因此,您不能依赖或设置特定的字段顺序.您唯一可以操作的是显示哪些字段而不显示哪些字段,请参见find的
MongoDB documents are BSON objects, unordered dictionaries of key-value pairs. So, you can't rely on or set a specific fields order. The only thing you can operate is which fields to display and which not to, see docs on find's projection argument.
也请参阅SO的相关问题:
Also see related questions on SO:
- MongoDB field order and document position change after update
- Can MongoDB and its drivers preserve the ordering of document elements
- Ordering fields from find query with projection
希望有帮助.
这篇关于修改属性在MongoDB中的显示顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!