Node.js Mongoose仅更新多个嵌套数组中已更改的数据字段
我有一个像这样的收藏Node.js Mongoose仅更新多个嵌套数组中已更改的数据字段,node.js,mongoose,Node.js,Mongoose,我有一个像这样的收藏 { "_id":"5f573a697775e75af4399afe", "basics":{ "location":{ "_id":"5f576a397775e75af4399b00", "address":"2712 Broadway St",
{
"_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
}
{
"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
}
]
}
我有一个api,它用类似这样的json响应
{
"_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
}
{
"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对象和更新的hidden
字段
现在,如何仅更新NodeJS Mongoose中更改的值,即一些隐藏的字段从true
更改为false
,反之亦然
我正在与合作,但无法为我的场景找到答案。正如您可能还看到的,位置
和配置文件
嵌套在基础
中,而其他配置文件则在架构之外
这是我的路线和登录
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);
}
}
)
})
}
有了这个控制器,我将不得不一个接一个地检查所有阵列(上面只显示了配置文件),并更新所有用户甚至没有更改的阵列。是否有其他方法可以只更新更改的数据
参考资料:
- -这提供javascript代码来获取两个对象(json或数组)之间的差异,但只提供更改的内容,在我的示例中,它只返回
隐藏的值,没有id
s
{"languages":[{"hidden":true}],"interests":[{"hidden":true}]}