MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

MongoDB - Importa dati

Usa mongoimport utility per importare dati in un database MongoDB.

MongoDB fornisce mongoimport utility che può essere utilizzata per importare file JSON, CSV o TSV in un database MongoDB.

mongoimport si trova nella directory bin (ad esempio, /mongodb/bin o ovunque tu l'abbia installato).

Per importare i dati, apri una nuova finestra Terminale/Prompt dei comandi e inserisci mongoimport seguito da parametri come il nome del database, il nome della raccolta, il nome del file di origine, ecc.

Se scopri che non puoi eseguire mongoimport , assicurati di essere uscito da mongo utilità o ha aperto una nuova finestra Terminale/Prompt dei comandi prima di eseguire mongoexport , poiché è un'utilità separata.

Importa file JSON

Ecco un esempio di esecuzione di mongoimport per importare un file JSON.

Potresti ricordare che in precedenza utilizzavamo mongoexport per esportare gli artisti raccolta in un file JSON.

Successivamente abbiamo abbandonato gli artisti raccolta del tutto.

Ora importeremo nuovamente quella raccolta nel nostro database.

mongoimport --db music --file /data/dump/music/artists.json

Messaggio risultante:

2016-07-12T13:34:04.904+0700	no collection specified
2016-07-12T13:34:04.905+0700	using filename 'artists' as collection
2016-07-12T13:34:04.911+0700	connected to: localhost
2016-07-12T13:34:04.968+0700	imported 13 documents

Se non specifichi un nome di raccolta, viene creata una raccolta in base al nome del file (meno qualsiasi estensione).

Ora torniamo al nostro mongo Finestra Terminale/Prompt dei comandi e recuperare l'elenco delle raccolte nel nostro database:

show collections

Risultato:

artists
musicians
producers

Ora interrogheremo la nostra raccolta rianimata.

db.artists.find()

Risultato:

{ "_id" : 1, "artistname" : "AC/DC" }
{ "_id" : ObjectId("5781d7f248ef8c6b3ffb014d"), "artistname" : "The Kooks" }
{ "_id" : ObjectId("5780fbf948ef8c6b3ffb0149"), "artistname" : "The Tea Party" }
{ "_id" : ObjectId("5781d7f248ef8c6b3ffb014f"), "artistname" : "Gang of Four" }
{ "_id" : ObjectId("5781d7f248ef8c6b3ffb014e"), "artistname" : "Bastille" }
{ "_id" : ObjectId("5781c9ac48ef8c6b3ffb014a"), "artistname" : "Jorn Lande" }
{ "_id" : ObjectId("5781f85d48ef8c6b3ffb0150"), "artistname" : "Deep Purple", "albums" : [ { "album" : "Machine Head", "year" : 1972, "genre" : "Rock" }, { "album" : "Stormbringer", "year" : 1974, "genre" : "Rock" } ] }
{ "_id" : ObjectId("578214f048ef8c6b3ffb0159"), "artistname" : "Miles Davis", "albums" : [ { "album" : "Kind of Blue", "year" : 1959, "genre" : "Jazz" }, { "album" : "Bitches Brew", "year" : 1970, "genre" : "Jazz" } ] }
{ "_id" : ObjectId("578217c248ef8c6b3ffb015a"), "artistname" : "Robben Ford", "albums" : [ { "album" : "Bringing it Back Home", "year" : 2013, "genre" : "Blues" }, { "album" : "Talk to Your Daughter", "year" : 1988, "genre" : "Blues" } ] }
{ "_id" : 2, "artistname" : "Prince", "address" : { "street" : "Audubon Road", "city" : "Chanhassen", "state" : "Minnesota", "country" : "United States" } }
{ "_id" : 4, "artistname" : "Rush" }
{ "_id" : 3, "artistname" : "Moby", "albums" : [ { "album" : "Play", "year" : 1999, "genre" : "Electronica" }, { "album" : "Long Ambients 1: Calm. Sleep.", "year" : 2016, "genre" : "Ambient" } ] }
{ "_id" : ObjectId("578217c248ef8c6b3ffb015b"), "artistname" : "Snoop Dogg", "albums" : [ { "album" : "Tha Doggfather", "year" : 1996, "genre" : "Rap" }, { "album" : "Reincarnated", "year" : 2013, "genre" : "Reggae" } ] }

Specifica un nome per la raccolta

Puoi usare il --collection argomento per fornire un nome della raccolta in cui devono essere inseriti i dati.

Importiamo un altro file, ma questa volta specifichiamo un nome per la raccolta:

mongoimport --db music --collection jazz --file /data/dump/music/miles_davis.json

Messaggio risultante:

2016-07-12T14:09:01.793+0700	connected to: localhost
2016-07-12T14:09:01.849+0700	imported 1 document

Ora torna a mongo e controlla l'elenco delle collezioni:

show collections

Messaggio risultante:

artists
jazz
musicians
producers

E infine, interroga il jazz collezione:

db.jazz.find().pretty()

Messaggio risultante:

{
	"_id" : ObjectId("578214f048ef8c6b3ffb0159"),
	"artistname" : "Miles Davis",
	"albums" : [
		{
			"album" : "Kind of Blue",
			"year" : 1959,
			"genre" : "Jazz"
		},
		{
			"album" : "Bitches Brew",
			"year" : 1970,
			"genre" : "Jazz"
		}
	]
}

Importa file CSV

Puoi importare un file CSV utilizzando --type csv .

Se il file CSV ha una riga di intestazione, utilizza --headerline per dire a mongoimport utilizzare la prima riga per determinare il nome dei campi nel documento risultante.

Se il file CSV non ha una riga di intestazione, utilizza i --fields parametro per impostare i nomi dei campi.

Con riga di intestazione

Ecco un esempio di importazione di un documento con una riga di intestazione.

Il contenuto del file CSV:

_id,albumname,artistname
1,Killers,"Iron Maiden"
2,Powerslave,"Iron Maiden"
12,"Somewhere in Time","Iron Maiden"
3,"Surfing with the Alien","Joe Satriani"
10,"Flying in a Blue Dream","Joe Satriani"
11,"Black Swans and Wormhole Wizards","Joe Satriani"
6,"Out of the Loop","Mr Percival"
7,"Suck on This",Primus
8,"Pork Soda",Primus
9,"Sailing the Seas of Cheese",Primus

Importa il file:

mongoimport --db music --collection catalog --type csv --headerline --file /data/dump/music/catalog.csv

Interroga la raccolta:

> db.catalog.find()
{ "_id" : 2, "albumname" : "Powerslave", "artistname" : "Iron Maiden" }
{ "_id" : 1, "albumname" : "Killers", "artistname" : "Iron Maiden" }
{ "_id" : 3, "albumname" : "Surfing with the Alien", "artistname" : "Joe Satriani" }
{ "_id" : 12, "albumname" : "Somewhere in Time", "artistname" : "Iron Maiden" }
{ "_id" : 10, "albumname" : "Flying in a Blue Dream", "artistname" : "Joe Satriani" }
{ "_id" : 6, "albumname" : "Out of the Loop", "artistname" : "Mr Percival" }
{ "_id" : 7, "albumname" : "Suck on This", "artistname" : "Primus" }
{ "_id" : 8, "albumname" : "Pork Soda", "artistname" : "Primus" }
{ "_id" : 11, "albumname" : "Black Swans and Wormhole Wizards", "artistname" : "Joe Satriani" }
{ "_id" : 9, "albumname" : "Sailing the Seas of Cheese", "artistname" : "Primus" }

Senza riga di intestazione

Ecco un altro file CSV, ma questo non ha una riga di intestazione:

Mutt Lange, 1948
John Petrucci, 1967
DJ Shadow, 1972
George Clinton, 1941

Ora lo importeremo e specificheremo i nomi dei campi da utilizzare:

mongoimport --db music --collection producers --type csv --fields name,born --file /data/dump/music/producers.csv

Interroga la raccolta:

> db.producers.find()
{ "_id" : 1, "name" : "Bob Rock" }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b72"), "name" : "John Petrucci", "born" : 1967 }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b73"), "name" : "Mutt Lange", "born" : 1948 }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b74"), "name" : "George Clinton", "born" : 1941 }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b75"), "name" : "DJ Shadow", "born" : 1972 }

Vedrai che ObjectId campo è stato creato e compilato automaticamente per noi.

Inoltre, avevamo già un documento in questa raccolta prima di eseguire l'importazione: { "_id" :1, "name" :"Bob Rock" } . Pertanto, puoi vedere che l'importazione è stata semplicemente aggiunto alla raccolta (anziché sostituirla e tutto il suo contenuto).

Puoi utilizzare lo stesso metodo per importare file TSV. Usa semplicemente --type tsv .