강의

멘토링

커뮤니티

Inflearn Community Q&A

ebonny0255's profile image
ebonny0255

asked

[Renewal] First-time MongoDB and NoSQL (Big Data) Database Bootcamp [From Beginner to Application] (Updated)

AND 조건이 적용되지 않아요~

Written on

·

226

0

강사님 질문이 있습니다.

다음 쿼리에서 AND 조건이 적용되지 않아요.

db_actor.find({'흥행지수': {'$nin': [9625, 8850]}, '흥행지수': {'$lt': 10000}})

를 실행하면 흥행지수가 9625, 8850 인 배우가 포함됩니다.

10000 이하인 것들이 그냥 $nin 조건 무시하고 다 나옵니다.

그래서 $and 를 직접 넣어서 돌려봤더니 9625, 8850 이 빠진 데이터가 제대로 나오더라고요. ㅜㅜ

db_actor.find({ '$and': [{'흥행지수': {'$nin': [9625, 8850]}}, {'흥행지수': {'$lt': 10000}}]})

어디 물어볼데도 없고 답답해서 여기에 질문 올립니다. ㅜㅜ

답변 좀 부탁드려요~ ^0^

pythonDBMS/RDBMSmongodb데이터 엔지니어링

Answer 1

0

funcoding님의 프로필 이미지
funcoding
Instructor

안녕하세요.  

다음과 같이 데이터가 들어가 있을 때,

{'_id': ObjectId('5fde9cffe93332cb943fa95f'), 'author': 'Mike', 'text': 'My second blog post!', 'tags': ['mongodb', 'python', 'pymongo']}
{'_id': ObjectId('5fde9d00e93332cb943fa960'), 'author': 'Mike', 'text': 'My second blog post!', 'tags': ['mongodb', 'python', 'pymongo']}
{'_id': ObjectId('5fde9d00e93332cb943fa961'), 'author': 'Mike', 'text': 'My second blog post!', 'tags': ['mongodb', 'python', 'pymongo']}
{'_id': ObjectId('5fde9d7de93332cb943fa962'), 'author': 'Mike', 'text': 'My first blog post!', 'tags': ['mongodb', 'python', 'pymongo']}
{'_id': ObjectId('5fde9d7de93332cb943fa963'), 'author': 'Mike', 'text': 'My first blog post!', 'tags': ['java', 'python']}
{'_id': ObjectId('5fde9d7de93332cb943fa964'), 'author': 'Mike', 'text': 'My first blog post!', 'tags': ['java', 'mongodb']}

다음과 같이 검색하면, 아무 데이터도 나오지 않습니다. nin 에 있는 데이터가 없어야 하고, 저자도 Mike 이어야 하는데, 그런 조건에 맞는 데이터가 없어서인 것이고요. 그래서 and 와 같이 동작합니다. 저도 테스트를 해보느라 직접 데이터를 다 넣어보았습니다만, 말씀하신 부분도 정상 동작해야 할 것으로 보여집니다.

docs = test_collection.find({'tags': {'$nin': ['mongodb', 'python']}, 'author': 'Mike'})

감사합니다.

ebonny0255's profile image
ebonny0255

asked

Ask a question