반응형
Mongo: 특정 필드가 없는 항목 찾기
MongoDB에서 특정 필드가 없는 문서를 검색하려면 어떻게 해야 합니까?
네, $exists를 사용하면 가능합니다.
db.things.find( { a : { $exists : false } } ); // return if a is missing
true일 경우 $exists는 필드가 포함된 문서(필드 값이 null인 문서 포함)와 일치합니다.가 false일 경우 쿼리는 필드를 포함하지 않는 문서만 반환합니다.
필드가 누락되어도 상관없습니다.null(혹은 그렇지 않은 경우)null그러면 보다 짧고 안전한 를 사용할 수 있습니다.
db.things.find( { a : null } ); // return if a is missing or null
그래서 더 안전하다.$exists돌아온다true필드가 늘인 경우에도 대부분의 경우 원하는 결과가 아니므로 NPE가 발생할 수 있습니다.
mongoose(v6)를 사용하고 있는 분들을 위해 여기에 있는 참고용입니다.$existsmongoose 스키마에서 정의되지 않은 필드를 찾기 위해 mongoose v6는 필드를 이스케이프합니다.
https://mongoosejs.com/docs/migrating_to_6.html#strictquery-is-removed-and-replaced-by-strict 를 참조해 주세요.
예를 들어 다음과 같습니다.
const userSchema = new Schema({ name: String });
const User = mongoose.model('User', userSchema);
// By default, this is equivalent to `User.find()` because Mongoose filters out `notInSchema`
await User.find({ notInSchema: 1 });
// Set `strictQuery: false` to opt in to filtering by properties that aren't in the schema
await User.find({ notInSchema: 1 }, null, { strictQuery: false });
// equivalent:
await User.find({ notInSchema: 1 }).setOptions({ strictQuery: false });
언급URL : https://stackoverflow.com/questions/5719408/mongo-find-items-that-dont-have-a-certain-field
반응형
'programing' 카테고리의 다른 글
| AngularJS 양식 유효성 검사 지시어 $set 요소 유효성 검사 (0) | 2023.03.31 |
|---|---|
| "wp_insert_post"를 실행하기 전에 게시물의 제목이 존재하는지 확인하여 게시물의 중복을 방지하는 Word press 방법 (0) | 2023.03.26 |
| 리액트 라우터 4에서의 앵커태그 사용 (0) | 2023.03.26 |
| 속성에 경로를 지정하여 클래스 속성을 JSON의 하위 속성에 매핑할 수 있습니까? (0) | 2023.03.26 |
| 여러 메타 키로 주문하는 방법 (0) | 2023.03.26 |