programing

MongoDB와 동일하지 않음

lovecodes 2023. 2. 12. 21:33
반응형

MongoDB와 동일하지 않음

MongoDB에서 텍스트 필드가 "(공백)이 아닌 쿼리를 표시하려고 합니다.

{ 'name' : { $not : '' }}

하지만 오류가 발생합니다.invalid use of $not

매뉴얼을 검토했습니다만, 사용 예시는 복잡한 케이스(regexp 및$not다른 연산자를 부정합니다).

내가 하려는 간단한 일을 어떻게 하겠어?

사용하다$ne--$not다음에 표준 연산자가 와야 합니다.

의 예$ne(즉, 동일하지 않음:

use test
switched to db test
db.test.insert({author : 'me', post: ""})
db.test.insert({author : 'you', post: "how to query"})
db.test.find({'post': {$ne : ""}})
{ "_id" : ObjectId("4f68b1a7768972d396fe2268"), "author" : "you", "post" : "how to query" }

그리고 지금$not술어를 받아들입니다.$ne를 무효로 합니다( ).$not):

db.test.find({'post': {$not: {$ne : ""}}})
{ "_id" : ObjectId("4f68b19c768972d396fe2267"), "author" : "me", "post" : "" }

여러 번 하고 싶은 경우$ne그럼 해라

db.users.find({name : {$nin : ["mary", "dick", "jane"]}})

사용하다$ne대신$not

http://docs.mongodb.org/manual/reference/operator/ne/ #op._S_ne

db.collections.find({"name": {$ne: ""}});

Mongo 문서에서:

$not연산자는 다른 연산자에게만 영향을 미치며 필드 및 문서를 개별적으로 확인할 수 없습니다.그럼, 를 사용해 주세요.$not논리분할 연산자 및$ne연산자는 필드의 내용을 직접 테스트합니다.

현장에서 직접 테스트하기 때문에$ne여기서 사용하는 것이 적절한 연산자입니다.

편집:

사용하고 싶은 상황$not다음과 같습니다.

db.inventory.find( { price: { $not: { $gt: 1.99 } } } )

그러면 다음과 같은 모든 문서가 선택됩니다.

  • 가격 필드 값이 1.99 이하이거나 가격입니다.
  • 필드가 존재하지 않습니다.

가 있는 경우null이를 회피하고 싶은 경우:

db.test.find({"contain" : {$ne :[] }}).pretty()

실제 예: 현재 사용자가 아닌 모든 사용자 검색:

var players = Players.find({ my_x: player.my_x,  my_y: player.my_y, userId: {$ne: Meteor.userId()} }); 

언급URL : https://stackoverflow.com/questions/9790878/mongodb-not-equal-to

반응형