Updating a field in a nested object when multiple conditions are met using Mongoose
I have a data model with nested objects, and I'd like to update the 'available' field to 'reserved' using the main object ID, plus the date and time associated with the nested object. Here's my data...
{
"_id": {
"$oid": "5bf1a675041f242cda138d1c"
},
"availability": [
{
"_id": {
"$oid": "5bf2c077d5683c38afacf34b"
},
"date": "2018-12-20 ",
"time": " 11:00 - 12:00 ",
"status": "reserved"
},
{
"_id": {
"$oid": "5bf2c3bb8b95bd3a7cb3eabe"
},
"date": "2018-11-30",
"time": "18:00 - 19:00",
"status": "available"
}
],
"name": "blah",
"price": 50,
"city": "blah",
"__v": 0
}
And the code I'm trying...
router.get('/reserve/:id', function(req, res, next){
//I've other stuff here that sets the correct variables according to the console.log, so str2[0] contains the date I want to find, etc.
Pitch.findOneAndUpdate({
id: req.params.id,
'availability.date': 'str2[0]', //passing in 2018-11-30
'availability.time': 'str2[1]', //passing in 18:00 - 19:00
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved'
}
})
Yet, the object won't update and set the status to 'reserved'. Can anyone spot what I'm doing wrong? Should I try to access the particular object using different conditions?
EDIT : Through trial and error, I think the problem is that the 'find' part returns the parent object, and all of its sub-objects in the 'availability' array are returned. The the 'update' part doesn't know which 'status' to update, and is failing at this point.
I was returning the date and time from a form in the front end and thought that I could identify the object in the array from it's parent ID plus the date and time of the reservation.
So I suppose my question should be, as the id returned in the URL is the parent object ID, is there a way to uniquely extract a specified availability object (eg. 5bf2c3bb8b95bd3a7cb3eabe) and update it's status to 'reserved'?
EDIT 2 : I can also 'find' an item by it's 'availability._id' but the full parent object is returned, so the 'update status' part doesn't work as it's too broad.
node.js mongodb express mongoose
add a comment |
I have a data model with nested objects, and I'd like to update the 'available' field to 'reserved' using the main object ID, plus the date and time associated with the nested object. Here's my data...
{
"_id": {
"$oid": "5bf1a675041f242cda138d1c"
},
"availability": [
{
"_id": {
"$oid": "5bf2c077d5683c38afacf34b"
},
"date": "2018-12-20 ",
"time": " 11:00 - 12:00 ",
"status": "reserved"
},
{
"_id": {
"$oid": "5bf2c3bb8b95bd3a7cb3eabe"
},
"date": "2018-11-30",
"time": "18:00 - 19:00",
"status": "available"
}
],
"name": "blah",
"price": 50,
"city": "blah",
"__v": 0
}
And the code I'm trying...
router.get('/reserve/:id', function(req, res, next){
//I've other stuff here that sets the correct variables according to the console.log, so str2[0] contains the date I want to find, etc.
Pitch.findOneAndUpdate({
id: req.params.id,
'availability.date': 'str2[0]', //passing in 2018-11-30
'availability.time': 'str2[1]', //passing in 18:00 - 19:00
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved'
}
})
Yet, the object won't update and set the status to 'reserved'. Can anyone spot what I'm doing wrong? Should I try to access the particular object using different conditions?
EDIT : Through trial and error, I think the problem is that the 'find' part returns the parent object, and all of its sub-objects in the 'availability' array are returned. The the 'update' part doesn't know which 'status' to update, and is failing at this point.
I was returning the date and time from a form in the front end and thought that I could identify the object in the array from it's parent ID plus the date and time of the reservation.
So I suppose my question should be, as the id returned in the URL is the parent object ID, is there a way to uniquely extract a specified availability object (eg. 5bf2c3bb8b95bd3a7cb3eabe) and update it's status to 'reserved'?
EDIT 2 : I can also 'find' an item by it's 'availability._id' but the full parent object is returned, so the 'update status' part doesn't work as it's too broad.
node.js mongodb express mongoose
add a comment |
I have a data model with nested objects, and I'd like to update the 'available' field to 'reserved' using the main object ID, plus the date and time associated with the nested object. Here's my data...
{
"_id": {
"$oid": "5bf1a675041f242cda138d1c"
},
"availability": [
{
"_id": {
"$oid": "5bf2c077d5683c38afacf34b"
},
"date": "2018-12-20 ",
"time": " 11:00 - 12:00 ",
"status": "reserved"
},
{
"_id": {
"$oid": "5bf2c3bb8b95bd3a7cb3eabe"
},
"date": "2018-11-30",
"time": "18:00 - 19:00",
"status": "available"
}
],
"name": "blah",
"price": 50,
"city": "blah",
"__v": 0
}
And the code I'm trying...
router.get('/reserve/:id', function(req, res, next){
//I've other stuff here that sets the correct variables according to the console.log, so str2[0] contains the date I want to find, etc.
Pitch.findOneAndUpdate({
id: req.params.id,
'availability.date': 'str2[0]', //passing in 2018-11-30
'availability.time': 'str2[1]', //passing in 18:00 - 19:00
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved'
}
})
Yet, the object won't update and set the status to 'reserved'. Can anyone spot what I'm doing wrong? Should I try to access the particular object using different conditions?
EDIT : Through trial and error, I think the problem is that the 'find' part returns the parent object, and all of its sub-objects in the 'availability' array are returned. The the 'update' part doesn't know which 'status' to update, and is failing at this point.
I was returning the date and time from a form in the front end and thought that I could identify the object in the array from it's parent ID plus the date and time of the reservation.
So I suppose my question should be, as the id returned in the URL is the parent object ID, is there a way to uniquely extract a specified availability object (eg. 5bf2c3bb8b95bd3a7cb3eabe) and update it's status to 'reserved'?
EDIT 2 : I can also 'find' an item by it's 'availability._id' but the full parent object is returned, so the 'update status' part doesn't work as it's too broad.
node.js mongodb express mongoose
I have a data model with nested objects, and I'd like to update the 'available' field to 'reserved' using the main object ID, plus the date and time associated with the nested object. Here's my data...
{
"_id": {
"$oid": "5bf1a675041f242cda138d1c"
},
"availability": [
{
"_id": {
"$oid": "5bf2c077d5683c38afacf34b"
},
"date": "2018-12-20 ",
"time": " 11:00 - 12:00 ",
"status": "reserved"
},
{
"_id": {
"$oid": "5bf2c3bb8b95bd3a7cb3eabe"
},
"date": "2018-11-30",
"time": "18:00 - 19:00",
"status": "available"
}
],
"name": "blah",
"price": 50,
"city": "blah",
"__v": 0
}
And the code I'm trying...
router.get('/reserve/:id', function(req, res, next){
//I've other stuff here that sets the correct variables according to the console.log, so str2[0] contains the date I want to find, etc.
Pitch.findOneAndUpdate({
id: req.params.id,
'availability.date': 'str2[0]', //passing in 2018-11-30
'availability.time': 'str2[1]', //passing in 18:00 - 19:00
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved'
}
})
Yet, the object won't update and set the status to 'reserved'. Can anyone spot what I'm doing wrong? Should I try to access the particular object using different conditions?
EDIT : Through trial and error, I think the problem is that the 'find' part returns the parent object, and all of its sub-objects in the 'availability' array are returned. The the 'update' part doesn't know which 'status' to update, and is failing at this point.
I was returning the date and time from a form in the front end and thought that I could identify the object in the array from it's parent ID plus the date and time of the reservation.
So I suppose my question should be, as the id returned in the URL is the parent object ID, is there a way to uniquely extract a specified availability object (eg. 5bf2c3bb8b95bd3a7cb3eabe) and update it's status to 'reserved'?
EDIT 2 : I can also 'find' an item by it's 'availability._id' but the full parent object is returned, so the 'update status' part doesn't work as it's too broad.
node.js mongodb express mongoose
node.js mongodb express mongoose
edited Nov 19 '18 at 20:07
asked Nov 19 '18 at 15:06
S-R
85
85
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Pitch.findOneAndUpdate({
id: req.params.id,
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved',
'availability.$.date': 'str2[0]', //passing in 2018-11-30
'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
}
})
Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
– S-R
Nov 19 '18 at 19:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377454%2fupdating-a-field-in-a-nested-object-when-multiple-conditions-are-met-using-mongo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Pitch.findOneAndUpdate({
id: req.params.id,
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved',
'availability.$.date': 'str2[0]', //passing in 2018-11-30
'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
}
})
Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
– S-R
Nov 19 '18 at 19:20
add a comment |
Pitch.findOneAndUpdate({
id: req.params.id,
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved',
'availability.$.date': 'str2[0]', //passing in 2018-11-30
'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
}
})
Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
– S-R
Nov 19 '18 at 19:20
add a comment |
Pitch.findOneAndUpdate({
id: req.params.id,
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved',
'availability.$.date': 'str2[0]', //passing in 2018-11-30
'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
}
})
Pitch.findOneAndUpdate({
id: req.params.id,
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved',
'availability.$.date': 'str2[0]', //passing in 2018-11-30
'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
}
})
answered Nov 19 '18 at 18:42


Raunik Singh
844
844
Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
– S-R
Nov 19 '18 at 19:20
add a comment |
Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
– S-R
Nov 19 '18 at 19:20
Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
– S-R
Nov 19 '18 at 19:20
Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
– S-R
Nov 19 '18 at 19:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377454%2fupdating-a-field-in-a-nested-object-when-multiple-conditions-are-met-using-mongo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown