먼저 내 문서의 구조 :Monk를 사용하여 Javascript의 forEach 루프에서 문서를 업데이트하는 방법은 무엇입니까?
{
"_id": "541a8bea74123744b371d38e",
"geometry": {
"type": "Point",
"coordinates": [
5.3435,
51.69554
]
},
"type": "Feature",
"properties": {
}
}
것은 내가 foreach 루프에서 업데이트 기능을 사용하여 필드를 추가 할 수 있습니다. 루프가 작동하지만 컬렉션의 모든 문서를 반복 할 수 있지만 업데이트 함수는 아무 작업도 수행하지 않습니다. 이것은 내가 가지고있는 것입니다 :
var counter = 0;
collection.find({}, {
stream: true
})
.each(function(doc) {
counter++;
var hash = geohash.encode(doc.geometry.coordinates[1], doc.geometry.coordinates[0], precision = 9);
console.log("id: " + doc._id + " hash: " + hash + " counter= " + counter);
collection.update({
_id: doc._id
}, {
$set: {
"properties.geohash.precision9": hash
}
},
function(err) {
if (err) console.log(err);
}
);
})
.error(function(err) {
// handle error
console.log(err);
})
.success(function(doc) {
// final callback
console.log("added geohash");
res.send({
objects: doc
});
});
내가 잘못 했나요?
을 것은이 방법은 업데이트를 저장하거나이 나 아닌 경우 MongoDB의에서 문서를 삽입 신분증. 따라서 Monk에서는 update 또는 insert 명령을 사용합니다. – justauser
** db.collection.update (item._id, item) **;를 사용할 수 있습니다. – Disposer