将 json 文件导入 mongo 的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10999023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:23:02  来源:igfitidea点击:

Proper way to import json file to mongo

jsonmongodbnosql

提问by Nahikariii

I've been trying to use mongo with some data imported, but I'm not able to use it properly with my document description.

我一直在尝试将 mongo 与导入的一些数据一起使用,但是我无法将它与我的文档描述一起正确使用。

This is an example of the .json I import using mongoimport: https://gist.github.com/2917854

这是我使用 mongoimport 导入的 .json 的示例:https://gist.github.com/2917854

mongoimport -d test -c example data.json

I noticed that all my document it's imported to a unique object in spite of creating one of object for each shop.

我注意到我的所有文档都被导入到一个独特的对象中,尽管为每个商店创建了一个对象。

That's why when I try to find a shop or anything I want to query, all the document is returned.

这就是为什么当我试图找到一家商店或任何我想查询的东西时,所有文档都被返回。

db.example.find({"shops.name":"x"})

I want to be able to query the db to obtain products by the id using dot notation something similar to:

我希望能够查询数据库以使用类似于以下内容的点表示法通过 id 获取产品:

db.example.find({"shops.name":"x","categories.type":"shirts","clothes.id":"1"}

The problem is that all the document is imported like a single object. The question is: How
do I need to import the object to obtain my desired result?

问题是所有文档都像单个对象一样导入。问题是:
我需要如何导入对象才能获得我想要的结果?

采纳答案by Eren Güven

Docsnote that:

文档指出:

This utility takes a single file that contains 1 JSON/CSV/TSV string per line and inserts it.

此实用程序采用一个包含每行 1 个 JSON/CSV/TSV 字符串的文件并将其插入。

In the structure you are using -assuming the errors on the gist are fixed- you are essentially importing one document with only shopsfield.

在您使用的结构中 - 假设要点上的错误是固定的 - 您实际上是在导入一个只有shops字段的文档。

After breaking the data into separate shop docs, import using something like (shops being the collection name, makes more sense than using example):

将数据分解为单独的商店文档后,使用类似的内容导入(商店是集合名称,比使用更有意义example):

mongoimport -d test -c shops data.json

and then you can query like:

然后你可以查询:

db.shops.find({"name":x,"categories.type":"shirts"})

回答by The Nail

There is a parameter --jsonArray:

有一个参数--jsonArray

Accept import of data expressed with multiple MongoDB document within a single JSON array
接受在单个 JSON 数组中导入用多个 MongoDB 文档表示的数据

Using this option you can feed it an array, so you only need to strip the outer object syntax i.e. everything at the beginning until and including "shops" :, and the }at the end.

使用这个选项,你可以给它一个数组,所以你只需要去掉外部对象的语法,即开头的所有内容,直到和包括"shops" :,以及}结尾。

Myself I use a little tool called jqthat can extract the array from command line:

我自己使用一个名为jq的小工具,它可以从命令行提取数组:

./jq '.shops' shops.json

回答by Mahesh K

IMPORT FROM JSON

从 JSON 导入

mongoimport --db "databaseName" --collection "collectionName" --type json --file "fileName.json" --jsonArray

JSON format should be in this format. (Array of Objects)

JSON 格式应该是这种格式。(对象数组)

[
    { name: "Name1", msg: "This is msg 1" },
    { name: "Name2", msg: "This is msg 2" },
    { name: "Name3", msg: "This is msg 3" }
]

IMPORT FROM CSV

从 CSV 导入

mongoimport --db "databaseName" --collection "collectionName" --type csv --file "fileName.csv" --headerline

More Info

更多信息

https://docs.mongodb.com/getting-started/shell/import-data/

https://docs.mongodb.com/getting-started/shell/import-data/

回答by xameeramir

Importing a JSON

导入 JSON

The command mongoimportallows us to import human readable JSONin a specific database & a collection. To import a JSONdata in a specific database & a collection, type mongoimport -d databaseName -c collectionName jsonFileName.json

该命令mongoimport允许我们JSON在特定数据库和集合中导入人类可读的内容。要导入JSON特定数据库和集合中的数据,请键入mongoimport -d databaseName -c collectionName jsonFileName.json