반응형
Mongoose에서 고유한 값을 쿼리하려면 어떻게 해야 합니까?
컬렉션을 위해 모든 고유 도시를 취득할 수 있어야 하는 문제가 있습니다. 내 코드는 다음과 같습니다.
var mongoose = require("mongoose"),
Schema = mongoose.Schema;
var PersonSchema = new Schema({
name: String,
born_in_city: String
});
var Person = mongoose.model('Person', PersonSchema);
네이티브 MongoDb에서는 할 수 있습니다.db.person.distinct("born_in_city")하지만 몽구스와 동등한 것은 없는 것 같다.직접 모든 문서를 반복하는 방법밖에 없습니까?아니면 더 나은 해결책이 있을까요?
기본을 사용하려고 합니다.node-mongodb-native대답자의 제안대로, 나는 이것을 시도했다.
mongoose.connection.db.collections(function(err, collections){
collections[0].distinct('born_in_city', function( err, results ){
console.log( err, results );
});
});
하지만, 그results비어 있고 오류는 없습니다.또, 무엇을 필터링 하는 것보다, 필요한 컬렉션만을 이름으로 취득할 수 있는 것을 선호합니다.collections가능한 한 돌아오다
Mongoose 3.x의 업데이트를 알려드리겠습니다.
MyModel.find().distinct('_id', function(error, ids) {
// ids is an array of all ObjectIds
});
내 프로그램에서는 이 코드가 작동한다.
Person.collection.distinct("born_in_city", function(error, results){
console.log(results);
});
node.discl v0.4.7, mongoose 1.3.3
소스 코드를 읽었는데 노드 몽고브 네이티브 드라이버가 클래스에 힘을 실어줍니다.접속 오브젝트에 대해서.mongoose.connect(mongodb://)를 실행한 후 이 작업을 시도할 수 있습니다.
if(mongoose.connections.length > 0) {
var nativeconn = mongoose.connections[0].conn;
nativeconn.person.distinct('born_in_city', function(error, results){
});
}
const findBornCity = async() => {
const bornCity = await Person.find().distinct("born_in_city")
return bornCity
}
언급URL : https://stackoverflow.com/questions/6043847/how-do-i-query-for-distinct-values-in-mongoose
반응형
'programing' 카테고리의 다른 글
| 스프링 부트 - Environment @Autowired가 Null Pointer를 슬로우합니다.예외. (0) | 2023.03.06 |
|---|---|
| Spring JPA Repository - jsonObject의 연산자 SIMPLE_PROPERTY에는 스칼라 인수가 필요합니다. (0) | 2023.03.06 |
| jQuery.getJSON - 접근컨트롤-허용-발신원 문제 (0) | 2023.03.06 |
| JSON의 이스케이프 문자 대체 (0) | 2023.03.06 |
| 모서리에서의 더블 버튼 클릭 방지/처리 (0) | 2023.03.06 |