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 I的一个示例: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:
我希望能够查询db以使用点表示法获取产品,类似于:
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?
问题是所有的文档都像单个对象一样导入。问题是:我需要如何导入对象以获得我想要的结果?
4 个解决方案
#1
37
Docs note 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 shops
field.
在您正在使用的结构中——假设要点上的错误是固定的——您实际上是在导入一个只有shop字段的文档。
After breaking the data into separate shop docs, import using something like (shops being the collection name, makes more sense than using example
):
在将数据分解成单独的商店文档之后,使用以下方法进行导入(商店是集合名,比使用示例更有意义):
mongoimport -d test -c shops data.json
and then you can query like:
然后你可以这样问:
db.shops.find({"name":x,"categories.type":"shirts"})
#2
23
There is a parameter --jsonArray
:
有一个参数——jsonArray:
Accept import of data expressed with multiple MongoDB document within a single JSON array
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 jq that can extract the array from command line:
我自己使用了一个叫做jq的小工具,可以从命令行提取数组:
./jq '.shops' shops.json
#3
8
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/
#4
0
Importing a JSON
The command mongoimport
allows us to import human readable JSON
in a specific database & a collection. To import a JSON
data in a specific database & a collection, type mongoimport -d databaseName -c collectionName jsonFileName.json
mongoimport命令允许我们在特定的数据库和集合中导入可读的JSON。要在特定的数据库和集合中导入JSON数据,输入mongoimport -d databaseName -c collectionName jsonFileName.json
#1
37
Docs note 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 shops
field.
在您正在使用的结构中——假设要点上的错误是固定的——您实际上是在导入一个只有shop字段的文档。
After breaking the data into separate shop docs, import using something like (shops being the collection name, makes more sense than using example
):
在将数据分解成单独的商店文档之后,使用以下方法进行导入(商店是集合名,比使用示例更有意义):
mongoimport -d test -c shops data.json
and then you can query like:
然后你可以这样问:
db.shops.find({"name":x,"categories.type":"shirts"})
#2
23
There is a parameter --jsonArray
:
有一个参数——jsonArray:
Accept import of data expressed with multiple MongoDB document within a single JSON array
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 jq that can extract the array from command line:
我自己使用了一个叫做jq的小工具,可以从命令行提取数组:
./jq '.shops' shops.json
#3
8
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/
#4
0
Importing a JSON
The command mongoimport
allows us to import human readable JSON
in a specific database & a collection. To import a JSON
data in a specific database & a collection, type mongoimport -d databaseName -c collectionName jsonFileName.json
mongoimport命令允许我们在特定的数据库和集合中导入可读的JSON。要在特定的数据库和集合中导入JSON数据,输入mongoimport -d databaseName -c collectionName jsonFileName.json