docker:docker pull mongo
docker volume create mongodbdata
docker run -d -p 27017:27017 -v mongodbdata:/data/db --name mongo6 mongo:latest
docker cp pessoas.json mymongo:/tmp/pessoas.json
docker exec mymongo mongoimport -d world -c pessoas --file /tmp/pessoas.json --jsonArray
Fazer o mesmo para a BD dos arqueossitios.
Executar a shell:
docker exec -it mongo6 mongosh
Listar as bases de dados: show dbs
Utilizar ou criar uma base de dados: use world
Listar coleções: show collections
Criar uma coleção:
db.createCollection("users")Inserir documentos:
db.users.insertOne({ username: "jcr", level: "admin"})db.users.insertMany([{ username: "maria", level: "consumer"}, { username: "pedro", level: "consumer"}])Procurar e selecionar dados: find() e findOne()
find({'atributos.gosta_dancar': true})find({'atributos.gosta_dancar': true}, {nome:1})Atualizar um documento: updateOne ou updateMany
db.users.updateOne( { username: "maria" }, { $set: { level: "producer" } } )db.users.updateMany({}, { $set: { level: "admin" } })Apagar documentos:
db.users.deleteOne({ username: "maria" })db.users.deleteMany({ level: "consumer" })Query Operators:
Comparaison:
$eq: Values are equal$ne: Values are not equal$gt: Value is greater than another value$gte: Value is greater than or equal to another value$lt: Value is less than another value$lte: Value is less than or equal to another value$in: Value is matched within an arrayLogical:
$and: Returns documents where both queries match$or: Returns documents where either query matches$nor: Returns documents where both queries fail to match$not: Returns documents where the query does not matchEvaluation:
$regex: Allows the use of regular expressions when evaluating field values$text: Performs a text search$where: Uses a JavaScript expression to match documents