기본 명령 MongoDB

요약


  • Select/create database
  • Insert/create collection/table with data
  • Show created databases
  • Show all collections in the database
  • Select data in the collection
  • Update data from one registry/document
  • Update all data from one registry/document
  • Delete data in the collection
  • Delete database

  • 데이터베이스 선택/생성




    use firstDatabase;
    


    데이터가 포함된 컬렉션/테이블 삽입/생성



    통사론




    //INSERT one data in the collection
    db.NameOfCollection.insertOne({"user": "admin", "password": "admin"});
    
    //INSERT several data in the collection
    db.NameOfCollection.insertMany([
      {"user": "admin", "password": "admin"},
      {"user": "admin", "password": "admin"},
      {"user": "admin", "password": "admin", "age": "10"}
    ]);
    


    예시




    //INSERT one data in the collection user
    db.user.insertOne({"user": "admin", "password": "admin"});
    
    //INSERT one data in the collection person
    db.person.insertOne({"name": "Bill Gates", "age": "66 "});
    
    //INSERT several data in the collection user
    db.user.insertMany([
      {"user": "admin1", "password": "admin1"}, 
      {"user": "admin2", "password": "admin2"},
      {"user": "admin1", "password": "password"}
    ]);
    
    /*INSERT several data in other collection
    in this case collection person*/
    db.person.insertMany([
      {"name": "Joao", "age": "10"}, 
      {"name": "Joao", "age": "20"},
      {"name": "Lucas", "age": "15"}
    ]);
    
    


    생성된 데이터베이스 표시



    일부 컬렉션에 항목이 삽입된 경우에만 데이터베이스 표시

    통사론




    show dbs;
    


    예시




    //Result example:
    admin    0.000GB
    config   0.000GB
    local    0.000GB
    firstDatabase  0.000GB
    


    데이터베이스의 모든 컬렉션 표시



    통사론




    show collections;
    


    예시




    //Result example:
    person
    user
    


    컬렉션에서 데이터 선택



    통사론




    //Select all the register of one collection equal to SELECT all
    db.NameOfCollection.find(); 
    
    //SELECT the first register in the collection
    db.NameOfCollection.findOne(); 
    
    //SELECT all when the parameter is true
    db.NameOfCollection.find({"WHERE"}); 
    
    //SELECT all with the result similar to a Json (Bson)
    db.NameOfCollection.find().pretty(); 
    


    예시




    //Data
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"), "user" : "admin1", "password" : "admin1" }
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca2"), "user" : "admin2", "password" : "admin2" }
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"), "user" : "admin1", "password" : "password" }
    
    //SELECT the first with user = admin1
    db.user.findOne({"user": "admin1"}); 
    
    //Result
    {
            "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"),
            "user" : "admin1",
            "password" : "admin1"
    }
    
    //SELECT all with user = admin1
    db.user.find({"user": "admin1"}); 
    
    //Result
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"), "user" : "admin1", "password" : "admin1" }
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"), "user" : "admin1", "password" : "password" }
    
    //SELECT all and return a Bson
    db.usuario.find().pretty(); 
    
    //Result
    {
            "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"),
            "user" : "admin1",
            "password" : "admin1"
    }
    {
            "_id" : ObjectId("62c4d5e11a23db9b29b9fca2"),
            "user" : "admin2",
            "password" : "admin2"
    }
    {
            "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"),
            "user" : "admin1",
            "password" : "password"
    }
    


    하나의 레지스트리/문서에서 데이터 업데이트



    통사론




    /*Update the data when the first parameter is true to the second parameter 
    don't change the struct*/
    
    //Update one when the first parameter is true
    db.NameOfCollection.updateOne({"WHERE"}, {$set:"SET"}); 
    
    //Update all when the first parameter is true
    db.NameOfCollection.updateMany({"WHERE"}, {$set:"SET"});
    


    예시




    //Data
    db.user.insertMany([
      {"user": "admin1", "password": "admin1"}, 
      {"user": "admin2", "password": "admin2"},
      {"user": "admin1", "password": "password"}
    ]);
    
    //UPDATE one when the first parameter is true
    db.firstDatabase.updateOne({"user" : "admin1"}, {$set: {"password": "new-password"}}); 
    
    //Result
    db.user.insertMany([
      {"user": "admin1", "password": "new-password"}, 
      {"user": "admin2", "password": "admin2"},
      {"user": "admin1", "password": "password"}
    ]);
    
    //UPDATE all when the first parameter is true
    db.user.updateMany({"user" : "admin1"}, {$set: {"password": "new-password"}}); 
    
    //Result
    db.user.insertMany([
      {"user": "admin1", "password": "new-password"}, 
      {"user": "admin2", "password": "admin2"},
      {"user": "admin1", "password": "new-password"}
    ]);
    


    하나의 레지스트리/문서에서 모든 데이터 업데이트



    통사론




    /*Change ALL data when the first parameter is true to the second parameter 
    change the struct, similar to ALTER TABLE*/
    
    //Change just the first when the parameter is true
    db.NameOfCollection.replaceOne({"WHERE"}, {"Nova estrutura"});
    


    replaceMany
    존재하지 않음

    예시




    //Data
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"), "user" : "admin1", "password" : "admin1" }
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca2"), "user" : "admin2", "password" : "admin2" }
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"), "user" : "admin1", "password" : "password" }
    
    /*Alter all data from the first when user = admin1
      to "password": "new-password"*/
    db.user.replaceOne({"user" : "admin1"}, {"password": "new-password"}); 
    
    //Result
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"), "password" : "new-password" }
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca2"), "user" : "admin2", "password" : "admin2" }
    { "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"), "user" : "admin1", "password" : "password" }
    


    컬렉션에서 데이터 삭제



    통사론




    //Delete just the first when the parameter is true
    db.NameOfCollection.deleteOne({"WHERE"});
    
    //Delete all data from the collection when the parameter is true
    db.NameOfCollection.deleteMany({"WHERE"});
    
    //Delete all data from the collection
    db.NameOfCollection.deleteMany({});
    


    예시




    //Data
    { "_id" : ObjectId("62c4d3121a23db9b29b9fc9e"), "name" : "Joao", "age" : "10" }
    { "_id" : ObjectId("62c4d3121a23db9b29b9fc9f"), "name" : "Joao", "age" : "20" }
    { "_id" : ObjectId("62c4d3121a23db9b29b9fca0"), "name" : "Lucas", "age" : "15" }
    
    //DELETE the first with name = Joao
    db.user.deleteOne({"name" : "Joao"}); 
    
    //Result
    { "_id" : ObjectId("62c4d3121a23db9b29b9fc9f"), "name" : "Joao", "age" : "20" }
    { "_id" : ObjectId("62c4d3121a23db9b29b9fca0"), "name" : "Lucas", "age" : "15" }
    
    //DELETE all data with name = Joao
    db.user.deleteMany({"name" : "Joao"}); 
    
    //Result
    { "_id" : ObjectId("62c4d3121a23db9b29b9fca0"), "name" : "Lucas", "age" : "15" }
    


    데이터베이스 삭제



    통사론




    //Select the database
    use database;
    //Delete it
    db.dropDatabase();
    

    좋은 웹페이지 즐겨찾기