将json文件导入mongo的正确方法 [英] Proper way to import json file to mongo

查看:368
本文介绍了将json文件导入mongo的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试对导入的某些数据使用mongo,但是我无法在我的文档说明中正确使用它.

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

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

This is an example of the .json I import using mongoimport: 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"})

我希望能够使用类似于以下内容的点符号查询数据库以获取ID的产品:

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

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?

推荐答案

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

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

在您正在使用的结构中-假设要点上的错误是固定的-您实际上是在仅具有shops字段的情况下导入一个文档.

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文档之后,使用类似的东西(shops是集合名称,比使用example更有意义)进行导入:

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"})

这篇关于将json文件导入mongo的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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