MongoDB
MongoDB 자주 쓰는 쿼리
ZzangHo
2022. 1. 26. 22:50
728x90
Group by
db.getCollection('컬렉션').aggregate([
{$group:
{
_id:"$필드명",
num_products:{$sum:1}
}
}
])
# 특정 조건 group핑
db.getCollection('컬렉션').aggregate([
{$match: {'필드명': '조건'}},
{$group:
{
_id:"$필드명",
num_products:{$sum:1}
}
}
])
필드 삭제
db.getCollection('컬렉션').update(
{'필드명': { '$exists': true }},
{'$unset': { '필드명': true }},
false,
true
)
카운트 조회
db.getCollection('컬렉션').find().count()
데이터 조회
# 단순 조회
db.getCollection('컬렉션').find()
# ObjectId 조회
db.getCollection('컬렉션').find({'_id': ObjectId('ObjectId')})
# 특정 조건 보다 큰 데이터 조회
db.getCollection('컬렉션').find({'필드명': {'$gt': 조건}})
# 특정 조건 보다 작은 데이터 조회
db.getCollection('컬렉션').find({'필드명': {'$lt': 조건}})
# 특정 필드 있는지?
db.getCollection('컬렉션').find({'필드명': {$exists: true}})
# 특정 필드 없는지?
db.getCollection('컬렉션').find({'필드명': {$exists: false}})
# and 조건
db.getCollection('컬렉션').find({ $and : [{'필드명1': '조건1'}, {'필드명2': '조건2'}]})
# or 조건
db.getCollection('컬렉션').find({ $or : [{'필드명1': '조건1'}, {'필드명2': '조건2'}]})