-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MongoDB import #542
Comments
I would suggest keeping the id column as is. |
For those looking to import this to their MongoDB, I wrote a script that can do that (but using prisma): import { Country, PrismaClient, State } from '@prisma/client';
import axios from 'axios';
/**
* We want to group items so that we can limit the number of
* asynchronous requests we send to the database
*/
function group<T = any>(items: T[], max: number) {
const grouped: T[][] = [];
do {
if (items.length > max) grouped.push(items.splice(0, max));
else grouped.push(items.splice(0));
} while (items.length > 0);
return grouped;
}
async function populate() {
const client = new PrismaClient();
await client.$connect();
const maxItems = 20;
async function populateCountries(_countries: any[]) {
const countriesGroups = group(_countries, maxItems);
for (const countries of countriesGroups) {
await Promise.all(
countries.map(async _country => {
const numericCode = String(_country.numeric_code);
const country = await client.country.upsert({
where: { numericCode },
create: {
name: _country.name,
iso2: String(_country.iso2),
iso3: String(_country.iso3),
numericCode,
phoneCode: String(_country.phone_code)
},
update: {
name: _country.name,
phoneCode: String(_country.phone_code)
}
});
await populateStates(_country.states, country);
})
);
}
console.log('Upserted countries');
}
async function populateStates(_states: any[], country: Country) {
const statesGroups = group(_states, maxItems);
for (const states of statesGroups) {
await Promise.all(
states.map(async (_state: any) => {
const ref = String(_state.id);
const state = await client.state.upsert({
where: { ref },
create: {
name: _state.name,
ref,
country: {
connect: {
id: country.id
}
}
},
update: {
name: _state.name
}
});
await populateCities(_state.cities, state);
})
);
}
console.log('Upserted states for', country.name);
}
async function populateCities(cities: any[], state: State) {
await client.$transaction(
cities.map(city => {
const ref = String(city.id);
return client.city.upsert({
where: { ref },
create: {
name: city.name,
ref,
state: {
connect: {
id: state.id
}
}
},
update: {
name: city.name
}
});
})
);
console.log('Upserted cities for', state.name);
}
try {
const { data } = await axios.get<any>(
'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/countries%2Bstates%2Bcities.json'
);
const start = Date.now();
await populateCountries(data);
console.log('took', (Date.now() - start) / 1000);
} catch (error) {
console.log(error);
await client.$disconnect();
}
}
populate(); This will also handle updates. I added a throttle to ensure that you don't overload your database because of all the async requests the script is sending, I ran it on my macos local to my mongodb docker container and I find that setting |
@dosubot write a script which will help convert the current db to mongodb |
Hey @dr5hn! Great to see you diving into the depths of data again 🚀😄 In MongoDB, the If you have an Here's a Python script that reads the JSON file, renames the import json
with open('countries.json', 'r') as f:
data = json.load(f)
for doc in data:
doc['_id'] = doc.pop('id')
with open('countries_mongodb.json', 'w') as f:
json.dump(data, f) You can then import the new JSON file into MongoDB using the mongoimport --db yourDbName --collection yourCollectionName --file countries_mongodb.json Please replace 'countries.json' and 'countries_mongodb.json' with the actual paths to your files, and replace 'yourDbName' and 'yourCollectionName' with the actual names of your MongoDB database and collection. Remember to do this for all the tables (countries, states, cities) you are importing. This approach will not create any issues as long as the I hope this helps! If you have any other questions, feel free to ask. Sources
|
@dr5hn That script saved my life. Btw don't forget |
Hi thank you very much for the library, its really useful. I tried importing the countries, states & cities tables in MongoDB but the issues is now I am having _id & id column.
Will it have any issues? Or should I rename id column to _id column before importing?
Any suggestions will really help me a lot.
The text was updated successfully, but these errors were encountered: