Terminal mongo to one-to-many basic examples

梦想与她 提交于 2019-12-10 10:37:39

问题


There are a lot of "why not JOIN?" questions here, to understand who to use mongodb... But even the guide at model-embedded-one-to-many-relationships-between-documents not show the basic clues...

There are to approaches:

  1. generate an array that contains the "embedded data" of the join, to produce "on fly" data. (using find() and what more simple algoritm?)

  2. use the db.collection.insert to produce "persistent data".

So, if I am at mongo terminal, what the simplest way to do that?


real-life dataset example

At https://github.com/datasets/country-codes we have

  • country-codes.csv: a table with ~250 rows.

  • datapackage.json: a complex structure with the country-codes table metadata, at resources.schema.fields.

So, before to do queries at mongo in the terminal, we can perform something like

wget -c https://raw.githubusercontent.com/datasets/country-codes/master/data/country-codes.csv
wget -c https://raw.githubusercontent.com/datasets/country-codes/master/datapackage.json

mongoimport -d ccodes_db -c ccodes --type csv --file country-codes.csv --headerline
mongoimport -d ccodes_db -c ccodes_meta  datapackage.json  --jsonArray

mongo
show dbs
use ccodes_db

So, lets "join" ccodes_meta with ccodes collections at mongo... The task is to embed fields name and description (of ccodes_meta) into the ccodes collection... With the simplest algorithms (not need best performance), see itens 1 and 2 of the question.

来源:https://stackoverflow.com/questions/32718079/terminal-mongo-to-one-to-many-basic-examples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!