1

我有一些测试数据正在尝试导入 MongoDB:

{ "name": "A3", "year": "1999", "parts": { "oil filter": { "type": 2, "partno": "123" }, "air filter":{ "type": 6, "partno": "gf6" } }}
{ "name": "A3", "year": "2000", "parts": { "oil filter": { "type": 2, "partno": "123" }, "air filter":{ "type": 6, "partno": "gf6" } }}
{ "name": "A3", "year": "2001", "parts": { "oil filter": { "type": 2, "partno": "123" }, "air filter":{ "type": 6, "partno": "gf6" } }}
{ "name": "A3", "year": "2002", "parts": { "oil filter": { "type": 2, "partno": "123" }, "air filter":{ "type": 6, "partno": "gf6" } }}
{ "name": "A3", "year": "2003", "parts": { "oil filter": { "type": 2, "partno": "557" }, "air filter":{ "type": 6, "partno": "gf4" } }}
{ "name": "A3", "year": "2004", "parts": { "oil filter": { "type": 2, "partno": "557" }, "air filter":{ "type": 6, "partno": "gf4" } }}

如果我使用 db.cars.insert() 插入行,所有嵌套字段都会完全按照显示的顺序出现。但是,如果我使用mongoimport导入这些行,则顺序会混乱:

{ "name" : "A3", "year" : "1999", "parts" : { "oil filter" : { "type" : 2, "partno" : "123" }, "air filter" : { "type" : 6, "partno" : "gf6" } } }
{ "name" : "A3", "year" : "2000", "parts" : { "air filter" : { "type" : 6, "partno" : "gf6" }, "oil filter" : { "type" : 2, "partno" : "123" } } }
{ "name" : "A3", "year" : "2001", "parts" : { "air filter" : { "type" : 6, "partno" : "gf6" }, "oil filter" : { "type" : 2, "partno" : "123" } } }
{ "name" : "A3", "year" : "2002", "parts" : { "oil filter" : { "type" : 2, "partno" : "123" }, "air filter" : { "type" : 6, "partno" : "gf6" } } }
{ "name" : "A3", "year" : "2003", "parts" : { "oil filter" : { "type" : 2, "partno" : "557" }, "air filter" : { "type" : 6, "partno" : "gf4" } } }
{ "name" : "A3", "year" : "2004", "parts" : { "oil filter" : { "type" : 2, "partno" : "557" }, "air filter" : { "type" : 6, "partno" : "gf4" } } }

这意味着如果我想检查哪些文档具有相同的嵌套“部分”,我无法正确比较它们。

> db.cars.aggregate([
... { $group: {
... _id: { name: "$name", parts: "$parts" },
... models_in_group: { $push: { year:"$year" } },
... } },
... ])
{ "_id" : { "name" : "A3", "parts" : { "air filter" : { "type" : 6, "partno" : "gf6" }, "oil filter" : { "type" : 2, "partno" : "123" } } },  "models_in_group" : [ { "year" : "2000" }, { "year" : "2001" } ] }
{ "_id" : { "name" : "A3", "parts" : { "oil filter" : { "type" : 2, "partno" : "557" }, "air filter" : { "type" : 6, "partno" : "gf4" } } },  "models_in_group" : [ { "year" : "2003" }, { "year" : "2004" } ] }
{ "_id" : { "name" : "A3", "parts" : { "oil filter" : { "type" : 2, "partno" : "123" }, "air filter" : { "type" : 6, "partno" : "gf6" } } },  "models_in_group" : [ { "year" : "1999" }, { "year" : "2002" } ] }

应该只有两组。那么,mongoimport 可以保持深度嵌套键的指定顺序吗?

4

1 回答 1

0

不幸的是,无法给出特定的顺序,这是因为您正在导入 JSON 对象而不是 JSON 对象数组。

我认为成功的唯一方法是编写一个为您执行此操作的例程。

于 2015-07-15T06:50:48.517 回答