ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • MongoDB Basics (in terminal)
    BackEnd/MongoDB 2021. 11. 12. 20:31

    설치과정은 버전이 바뀔 때마다 업데이트되고 Mac, Windows 에 따라 다르므로 공식 문서 또는 Youtube, Stack overflow 참고 바람..

     

    mongo

    start database 

     

    use <database_name>

    change working database. If it doesn't exist, make new one 

     

    show dbs

    show all the databases.

     

    show collections

    In the current working database, show all the collections.

     

    db.<collectionName>,insertOne({})

     

    db.<collectionName>.insertMany([{}, {}]

    db.tours.insertMany(
    	[
    		{name: "The Sea Explorer", price: 497, rating: 4.8}, 
    		{name: "The Snow Adventurer", price: 997, rating: 4.9, difficulty: "easy"}
    	]
    )

    In the current working database, in collection named 'tours', insert document

     

    db.tours.find()

    In the current working database, in collection named 'tours', show documents 

     

    db.<collectionName>.find({}) 

    find specific douments using filter in find({})

     db.tours.find({name: "The Forest Hiker"})

     

    deeper search query: 

    db.tours.find({price: {$lte: 500}})

    lte: less than or equal to

     

    && (AND) conditions: 

     

     db.tours.find({ price: {$lt: 500}, rating: {$gte: 4.8}})

     

    || (OR) conditions: 

    db.tours.find({ $or: 
    	[{price: {$lt:500}}, {rating: {$lte: 4.8}}] 
    })

     

    검색 결과 중 일부만 출력하기

    db.tours.find({ $or: [{price: {$lt:500}}, {rating: {$gte: 4.8}}] }, {name: 1})

     

    Update

    1. update 할 target 먼저 검색

    2. $set 을 이용해서 어떻게 update 할지 설정

    db.tours.updateOne({name: "The Snow Adventurer"}, {$set: {price: 597}})

     

    db.tours.updateMany({price: {$gte: 500}, rating: {$gte: 4.8}}, {$set: {premium: true}})

     

    이거 이외에 replaceOne 이런 것도 있음. 

     

     

    Delete

    db.tours.deleteMany({rating: {$lt: 4.8}})

     

    db.tours.deleteMany({})

     

     

    quit()

    close mongoDB

     

     

     

    'BackEnd > MongoDB' 카테고리의 다른 글

    MongoDB 관련 tool , Website  (0) 2021.11.12
Designed by Tistory.