我有一个像这样的收藏
{
"_id":"5f573a697775e75af4399afe",
"basics":{
"location":{
"_id":"5f576a397775e75af4399b00",
"address":"2712 Broadway St",
"postalCode":"CA 94115",
"city":"San Francisco",
"countryCode":"US",
"region":"California",
"hidden":"false"
},
"name":"John Doe",
"label":"Programmer",
"picture":"",
"email":"john@gmail.com",
"phone":"(912) 555-4321",
"website":"http://johndoe.com",
"summary":"A summary of John Doe...",
"profiles":[
{
"_id":"5f573a697775e75af4399aff",
"network":"Twitter",
"username":"john",
"url":"http://twitter.com/john",
"hidden": false
}
]
},
"work":[
{
"highlights":[
"Started the company"
],
"_id":"5f573a697775e75af4399b00",
"company":"Company",
"position":"President",
"website":"http://company.com",
"startDate":{
"$date":"2013-01-01T00:00:00.000Z"
},
"endDate":{
"$date":"2014-01-01T00:00:00.000Z"
},
"summary":"Description...",
"hidden": false
}
],
"volunteer":[
{
"highlights":[
"Awarded 'Volunteer of the Month'"
],
"_id":"5f573a697775e75af4399b01",
"organization":"Organization",
"position":"Volunteer",
"website":"http://organization.com/",
"startDate":{
"$date":"2012-01-01T00:00:00.000Z"
},
"endDate":{
"$date":"2013-01-01T00:00:00.000Z"
},
"summary":"Description...",
"hidden": false
}
],
"education":[
{
"courses":[
"DB1101 - Basic SQL",
"MTH 511 - Algorithmic Mathematics"
],
"_id":"5f573a697775e75af4399b02",
"institution":"University",
"area":"Software Development",
"studyType":"Bachelor",
"startDate":{
"$date":"2011-01-01T00:00:00.000Z"
},
"endDate":{
"$date":"2013-01-01T00:00:00.000Z"
},
"gpa":"4.0",
"hidden": false
}
],
"awards":[
{
"_id":"5f573a697775e75af4399b03",
"title":"Award",
"date":{
"$date":"2014-11-01T00:00:00.000Z"
},
"awarder":"Company",
"summary":"There is no spoon.",
"hidden": false
}
],
"publications":[
{
"_id":"5f573a697775e75af4399b04",
"name":"Publication",
"publisher":"Company",
"releaseDate":{
"$date":"2014-10-01T00:00:00.000Z"
},
"website":"http://publication.com",
"summary":"Description...",
"highlights":[
"Wrote this paper"
],
"hidden": false
}
],
"skills":[
{
"keywords":[
"HTML",
"CSS",
"Javascript"
],
"_id":"5f573a697775e75af4399b05",
"name":"Web Development",
"level":"Master",
"hidden": false
}
],
"languages":[
{
"_id":"5f573a697775e75af4399b06",
"language":"English",
"fluency":"Native speaker",
"hidden": false
}
],
"interests":[
{
"keywords":[
"Ferrets",
"Unicorns"
],
"_id":"5f573a697775e75af4399b07",
"name":"Wildlife",
"hidden": false
}
],
"references":[
{
"_id":"5f573a697775e75af4399b08",
"name":"Jane Doe",
"reference":"Reference...",
"hidden": false
}
],
"__v":0
}
我有一个api,它用这样的json响应
{
"id": "5f573a697775e75af4399afe",
"quickText": "John Doe",
"hidden": false,
"location": [
{
"_id":"5f576a397775e75af4399b00",
"quickText": "2712 Broadway St",
"hidden": false
}
],
"profiles": [
{
"id": "5f573a697775e75af4399aff",
"quickText": "Twitter",
"hidden": false
}
],
"work": [
{
"id": "5f573a697775e75af4399b00",
"quickText": "Company / President",
"hidden": false
}
],
"volunteer": [
{
"id": "5f573a697775e75af4399b01",
"quickText": "Organization / Volunteer",
"hidden": false
}
],
"education": [
{
"id": "5f573a697775e75af4399b02",
"quickText": "University / Software Development",
"hidden": false
}
],
"publications": [
{
"id": "5f573a697775e75af4399b04",
"quickText": "Publication",
"hidden": false
}
],
"awards": [
{
"id": "5f573a697775e75af4399b03",
"quickText": "Award",
"hidden": false
}
],
"skills": [
{
"id": "5f573a697775e75af4399b05",
"quickText": "Web Development",
"hidden": false
}
],
"languages": [
{
"id": "5f573a697775e75af4399b06",
"quickText": "English",
"hidden": false
}
],
"interests": [
{
"id": "5f573a697775e75af4399b07",
"quickText": "Wildlife",
"hidden": false
}
],
"references": [
{
"id": "5f573a697775e75af4399b08",
"quickText": "Jane Doe",
"hidden": false
}
]
}
在前端用户更改
hidden
字段中的任意一个
true
或
false
并返回相同的json对象和更新的
隐藏的
领域。
现在,我如何只更新NodeJS Mongoose中更改的值,即一些
隐藏的
字段从
符合事实的
到
错误的
反之亦然。
我和
Array Update Operators
但我想不出我的情况。正如你可能也看到的,
location
和
profiles
嵌套在
basics
然而,其他人则在模式之外。
这是我的路线和登录,
router.post('/hidden/:id', function(req, res, next) {
let changedResume = req.body
if(changedResume.profiles.length) {
console.log('there is profile')
changedResume.profiles.forEach(elem => {
console.log(elem)
Resume.findByIdAndUpdate(
{ "_id" : new ObjectId(changedResume.id), "basics.profiles._id": new ObjectId(elem.id)},
{ "$set": { "basics.profiles.$[element].hidden": elem.hidden}},
{ "arrayFilters": [{"element._id": new ObjectId(elem.id)}], "upsert": true},
function(err, docs) {
if (err){
console.log(err)
}
else{
console.log("Updated User : ", docs);
}
}
)
})
}
有了这个控制器,我将不得不一个接一个地检查所有阵列(上面只显示了配置文件),并更新所有用户甚至没有更改的阵列。有没有其他方法可以只更新更改的数据?
参考资料: