Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

mongodb - Proper way to import json file to 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.

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Docs note that:

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...