我一直在尝试使用 mongo 导入一些数据,但我无法在我的文档描述中正确使用它。
这是我使用 mongoimport 导入的 .json 示例:https://gist.github.com/2917854
mongoimport -d test -c example data.json
我注意到,尽管为每个商店创建了一个对象,但我的所有文档都被导入到一个唯一的对象中。
这就是为什么当我尝试查找商店或任何我想查询的东西时,所有文档都会返回。
db.example.find({"shops.name":"x"})
我希望能够使用点符号查询数据库以通过 id 获取产品:
db.example.find({"shops.name":"x","categories.type":"shirts","clothes.id":"1"}
问题是所有文档都像单个对象一样导入。问题是:如何
我需要导入对象以获得我想要的结果吗?
最佳答案
Docs请注意:
This utility takes a single file that contains 1 JSON/CSV/TSV string per line and inserts it.
在您使用的结构中-假设要点上的错误已修复-您实际上是在导入一个只有 shops
字段的文档。
将数据分解为单独的商店文档后,使用类似(商店是集合名称,比使用 example
更有意义)导入:
mongoimport -d test -c shops data.json
然后你可以像这样查询:
db.shops.find({"name":x,"categories.type":"shirts"})
https://stackoverflow.com/questions/10999023/