dbName.collectionName
형태mongodump --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"
mongoexport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --collection=sales --out=sales.json
mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --drop dump
mongoimport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --drop sales.json
brew install mongosh
Homebrew 에러 발생 시 (삭제 후 재설치)
homebrew-core is a shallow clone 에러 발생 시, 화면에 안내 된 명령을 실행해도 설치가 진행되지 않는 경우
- 삭제 스크립트 실행
/bin/bash -c "$(curl -fsSL [https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh](https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh))"
- 파일 완전 삭제
sudo rm -rf /usr/local/Homebrew
- 설치 스크립트 실행
/bin/bash -c "$(curl -fsSL [https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh](https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh))"
mongosh "mongodb+srv://<id>:<uri>/admin" --apiVersion 1
show dbs
: 모든 데이터베이스 목록 조회use <dbName>
: 해당 db를 기본으로 사용하도록 설정show collections
: 현재 db에 있는 모든 collection 목록 조회db.<collectionName>.find({ query })
: 현재 사용 중인 db의 collection에서 쿼리 실행find().pretty()
: 실행하면 쿼리 결과를 예쁘게 보여주는데, mongosh 에서는 기본으로 제공하는 듯db.inspections.insert([{"_id":1, "test":1}, {"_id":1, "test":2}, {"_id":3, "test":3}])
{ "ordered": false }
)db.inspections.insert([{"_id":1, "test":1}, {"_id":1, "test":2}, {"_id":3, "test":3}], {"ordered":false})
// equal
db.collection.find({"field": {"$eq": "value"}})
db.collection.find({"field": "value"})
// less than, less than or equal
db.collection.find({"field": {"$lt": number}})
db.collection.find({"field": {"$lte": number}})
// greater than, greater than or equal
db.collection.find({"field": {"$gt": number}})
db.collection.find({"field": {"$gte": number}})
// and
db.collection.find({"$and" : [{"field1": "value1"}, {"field2": "value2"}]})
db.collection.find({"field1": "value1", "field2": "value2"})
db.collection.find({"$and" : [{"field": {"$gt": 25}}, {"field": {"$lt": 100}}]})
db.collection.find({"field": {"$gt": 25, "$lt": 100}})
// or, nor
db.collection.find({"$or": [{"field1": "value1"}, {"field2": "value2"}]})
db.collection.find({"$nor": [{"field1": "value1"}, {"field2": "value2"}]})
// not
db.collection.find({"usertype": {"$not": {"$eq":"Subscriber"}}})
$and
연산자는 동일한 연산자를 두 번 이상 포함해야 할 때 사용$and
연산자를 사용하지 않으면 엉뚱한 값이 나오게 됨db.routes.find({
"$and": [
{ "$or" :[ { "dst_airport": "KZN" }, { "src_airport": "KZN" }] },
{ "$or" :[ { "airplane": "CR2" }, { "airplane": "A81" } ] }
]
})
{"$expr": {"$eq": ["$start station id", "$end station id"]}}
$
는 다양한 용도로 사용 됨
- operator를 사용할 때 (예 : $and, $or, etc...)
- field 값의 주소를 나타낼 때 (예: “$start station id”)
db.collection.find({"arrayField": "value1"})
// ["value1", "value2"]
// ["value3", "value1"]
// ["value4", "value3", "value2", "value1"]
// 이러한 문서들이 모두 반환 됨
db.collection.find({"arrayField":{"$all":["value1", "value2", "value4", "value3"]}})
// 저장이 ["value1", "value2", "value3", "value4"]로 되어 있어도 검색 됨
1
: 반환하려는 Field를 지정0
: 반환하고 싶은 않은 Field를 지정0, 1
을 동시에 사용할 수 없다. 단, _id를 제외할 때에는 0을 아무 때나 사용할 수 있다.db.collection.find({ <query> }, { <projection> })
db.collection.find({ <query> }, { <field1>: 1, <field2>: 1 })
db.collection.find({ <query> }, { <field1>: 0, <field2>: 0 })
db.collection.find({ <query> }, { <field1>: 1, "_id": 0 })
db.grades.find({"class_id": 431}, {"scores":{"$elemMatch":{"score":{"$gt":85}}}})
{
_id: ObjectId("56d5f7ec604eb380b0d8f43c"),
scores: [ { type: 'exam', score: 93.95060569368262 } ]
},
{
_id: ObjectId("56d5f7ec604eb380b0d8f76c"),
scores: [ { type: 'homework', score: 93.31790089774611 } ]
},
{ _id: ObjectId("56d5f7ec604eb380b0d8f8ea") },
{ _id: ObjectId("56d5f7ec604eb380b0d8fb98") },
{ _id: ObjectId("56d5f7ec604eb380b0d8fc0c") },
{ _id: ObjectId("56d5f7ec604eb380b0d8fd69") }
.
을 이용해서 Sub-Document에 접근할 수 있음Dot notation vs elemMatch : https://techbrij.com/mongodb-query-elemmatch-dot-notation
dot notation을 이용할 경우 조건이 2개 이상이 될 경우, 하나의 element가 조건을 모두 만족하는지 체크하는 것이 아니라 하나의 조건만 맞아도 반환이 된다. 그러나 elemMatch의 경우 2개 이상의 조건이 있는 경우, 해당 조건을 모두 만족하는 element를 찾는다.
$sum
을 이용해서 count를 구함db.listingsAndReviews.aggregate([
{"$project": {"address":1,"_id":0 }},
{"$group":{
"_id":"$address.country", "count":{"$sum":1}
}
}
]);
db.trips.createIndex({ "birth year": 1 })
db.trips.createIndex({ "start station id": 1, "birth year": 1 })
// 아래 쿼리를 실행할 때 single field index 보다 compound index가 더 도움된다.
db.trips.find({ "start station id": 476 }).sort( { "birth year": 1 } )
db.collection.updateOne({<query>}, {<update>}, {"upsert": true})