0
국가 배열 내부에 도시를 삽입하려고하는데, 다소 혼란 스럽습니다. 나는 평균을 사용하고있다. 나는 새로운 도시를 삽입 할 때, 나는 cities
내부 states
를 업데이트하고 다른 하나를 밀어 필요내부 문서를 nodejs와 함께 mongodb에 삽입하십시오.
var countrySchema = {
countryName: { type: String, required: true },
loc: [{ type: Number, required: true }],
states: [{
stateName: { type: String, required: true },
loc: [{ type: Number, required: true }],
cities: [{
cityName: { type: String, required: true },
loc: [{type: Number, required: true}]
}]
}]
};
:이 MongoDB의 모델입니다.
나는 POST의 metod을 사용하고 있는데, 이는 다음과 같다 : 쿼리는 대부분의 아마
module.exports.newCity = function (req, res, Location) { try { var locations = req.body.locations; } catch (error) { return res.status(status.BAD_REQUEST).json({error: error.toString()}); } Location.update({"countryName": locations.countryName, "states.stateName": locations.states.stateName}, {'$push': {'cities': locations.states.cities}}, function (error, city) { if (error) { return res.status(status.INTERNAL_SERVER_ERROR).json({error: error.toString()}); } if (!city) { return res.status(status.NOT_FOUND).json({error: error.toString()}); } res.json({city: city}) }); };
:
api.post('/city/', wagner.invoke(function (Location) { return function (req, res) { return require('./Controllers/locationController').newCity(req, res, Location); }; }));
그리고이 newCity
기능입니다 업데이트 기능에 대해 잘못 알고 있습니다.
나는 요청으로 추적의 JSON을 보내 해요 :
{ "locations": { "countryName": "Mexico", "loc": [1, 2], "states": { "stateName": "test one", "loc": [1, 2], "cities": { "cityName": "City for test one", "loc": [1, 2] } } } }
그리고 나는이 응답을 얻을 :
나는projection operator
내 문제를 해결
{
"city": {
"ok": 0
"n": 0
"nModified": 0
}
}