Push to last element of sub-arrayCan you have mongo $push prepend instead of append?How to Update Multiple Array Elements in mongodbmongodb push element into array of objectsGetting first and last element of array in MongoDBMongodb update deeply nested subdocumentMongo Aggregation select and push last element in arrayHow to filter sub array and project multiple matching values of a matching document in mongoDbmongodb: How to create _id for each array element?Pushing into sub document array inside by elementmongodb: push to subarray of array element or add to array if not existingMongoDB push record in array

Why aren't satellites disintegrated even though they orbit earth within earth's Roche Limits?

Warped chessboard

On a piano, are the effects of holding notes and the sustain pedal the same for a single chord?

Have the writers and actors of Game Of Thrones responded to its poor reception?

Was murdering a slave illegal in American slavery, and if so, what punishments were given for it?

How does the "reverse syntax" in Middle English work?

Is my company merging branches wrong?

What city and town structures are important in a low fantasy medieval world?

How could Dwarves prevent sand from filling up their settlements

How to fix "webpack Dev Server Invalid Options" in Vuejs

Greek theta instead of lower case þ (Icelandic) in TexStudio

How do I unravel apparent recursion in an edef statement?

Can I have a delimited macro with a literal # in the parameter text?

Find the 3D region containing the origin bounded by given planes

Parse a C++14 integer literal

Are there any crystals that are theoretically possible, but haven't yet been made?

Why are Marine Le Pen's possible connections with Steve Bannon something worth investigating?

In how many ways can we partition a set into smaller subsets so the sum of the numbers in each subset is equal?

Bash - Execute two commands and get exit status 1 if first fails

Have I found a major security issue with login

What's is the easiest way to purchase a stock and hold it

Why does snapping your fingers activate the Infinity Gauntlet?

How do you cope with rejection?

Why could the Lunar Ascent Engine be used only once?



Push to last element of sub-array


Can you have mongo $push prepend instead of append?How to Update Multiple Array Elements in mongodbmongodb push element into array of objectsGetting first and last element of array in MongoDBMongodb update deeply nested subdocumentMongo Aggregation select and push last element in arrayHow to filter sub array and project multiple matching values of a matching document in mongoDbmongodb: How to create _id for each array element?Pushing into sub document array inside by elementmongodb: push to subarray of array element or add to array if not existingMongoDB push record in array






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















My document looks like this:




_id: ...
checkins: [
...
...

...
activities: [
...
...
]

]



If I want to push into the first element of the checkins array I could do this:



mycol.updateOne(
'_id': ...,
'$push':
'checkins.0.activities': entry
)


But I need to insert entry into the last index of the checkins array, so how could I implement something like this?



mycol.updateOne(
'_id': ...,
'$push':
'checkins.-1.activities': entry
)


Edit:



I can't just do:



'$set': 'checkins.-1': <entry>


Because it'd overwrite the other elements in the array, and this:



'$set': 'checkins.-1': [...otherEntries, entry]


Would mean inserting the whole array every single time, and since it's very big array, it'd be way too expensive



This, as suggested, looks promising, though:




$each: [ entry ],
$position: -1



But this just lets me push into the checkins array, if I wanted to add an element to the activities subarray it'd involve inserting the whole array, just like the first solution. In this case, I'm looking for something like this:




$each: '$push': 'activities': entry ,
$position: -1



But of course it does not work, mongo complains that




The argument to $each in $push must be an array but it was of type: object




How do I push into the last element, i.e. into checkins.-1.activities?










share|improve this question
























  • Possible duplicate of Can you have mongo $push prepend instead of append?

    – manish kumar
    Mar 23 at 18:44











  • Not quite, the syntax doesn't work the same when dealing with nested arrays. Also, I'd rather not $set the entire activities array every time I need to push one activity

    – arielnmz
    Mar 23 at 18:50







  • 1





    Nested arrays are really bad. Basically impossible to atomically update before relatively recent changes for MongoDB, and mostly they just don't deliver as a structure what you "think" you designed them for. Typically people think "like a relation", but it's not actually how you do it. The -1 problem has long be dealt with by basically "reversing the order of the array", which is the real use case of $position and $sort modifiers. You would be better of actually showing you full use case, since the solution would certainly be to "flatten" the arrays and reverse the order.

    – Neil Lunn
    Mar 23 at 23:54






  • 1





    In other words "show what actually needs to get done" rather than discuss how you think it needs to be done. And be prepared to make structural changes to data.

    – Neil Lunn
    Mar 23 at 23:56











  • show what actually needs to get done Its literally in the two examples. I had to add why they don't work because the site prompted me to add why the suspected dupe isn't a dupe. Typically people think "like a relation" that's totally not what I think, please if you need more info ask for it but don't start a discussion. The -1 problem has long be dealt with by basically "reversing the order of the array" So, how does one reverse the order of an embedded array within another array? Or use $position and $sort within arrays?

    – arielnmz
    Mar 25 at 17:02


















0















My document looks like this:




_id: ...
checkins: [
...
...

...
activities: [
...
...
]

]



If I want to push into the first element of the checkins array I could do this:



mycol.updateOne(
'_id': ...,
'$push':
'checkins.0.activities': entry
)


But I need to insert entry into the last index of the checkins array, so how could I implement something like this?



mycol.updateOne(
'_id': ...,
'$push':
'checkins.-1.activities': entry
)


Edit:



I can't just do:



'$set': 'checkins.-1': <entry>


Because it'd overwrite the other elements in the array, and this:



'$set': 'checkins.-1': [...otherEntries, entry]


Would mean inserting the whole array every single time, and since it's very big array, it'd be way too expensive



This, as suggested, looks promising, though:




$each: [ entry ],
$position: -1



But this just lets me push into the checkins array, if I wanted to add an element to the activities subarray it'd involve inserting the whole array, just like the first solution. In this case, I'm looking for something like this:




$each: '$push': 'activities': entry ,
$position: -1



But of course it does not work, mongo complains that




The argument to $each in $push must be an array but it was of type: object




How do I push into the last element, i.e. into checkins.-1.activities?










share|improve this question
























  • Possible duplicate of Can you have mongo $push prepend instead of append?

    – manish kumar
    Mar 23 at 18:44











  • Not quite, the syntax doesn't work the same when dealing with nested arrays. Also, I'd rather not $set the entire activities array every time I need to push one activity

    – arielnmz
    Mar 23 at 18:50







  • 1





    Nested arrays are really bad. Basically impossible to atomically update before relatively recent changes for MongoDB, and mostly they just don't deliver as a structure what you "think" you designed them for. Typically people think "like a relation", but it's not actually how you do it. The -1 problem has long be dealt with by basically "reversing the order of the array", which is the real use case of $position and $sort modifiers. You would be better of actually showing you full use case, since the solution would certainly be to "flatten" the arrays and reverse the order.

    – Neil Lunn
    Mar 23 at 23:54






  • 1





    In other words "show what actually needs to get done" rather than discuss how you think it needs to be done. And be prepared to make structural changes to data.

    – Neil Lunn
    Mar 23 at 23:56











  • show what actually needs to get done Its literally in the two examples. I had to add why they don't work because the site prompted me to add why the suspected dupe isn't a dupe. Typically people think "like a relation" that's totally not what I think, please if you need more info ask for it but don't start a discussion. The -1 problem has long be dealt with by basically "reversing the order of the array" So, how does one reverse the order of an embedded array within another array? Or use $position and $sort within arrays?

    – arielnmz
    Mar 25 at 17:02














0












0








0








My document looks like this:




_id: ...
checkins: [
...
...

...
activities: [
...
...
]

]



If I want to push into the first element of the checkins array I could do this:



mycol.updateOne(
'_id': ...,
'$push':
'checkins.0.activities': entry
)


But I need to insert entry into the last index of the checkins array, so how could I implement something like this?



mycol.updateOne(
'_id': ...,
'$push':
'checkins.-1.activities': entry
)


Edit:



I can't just do:



'$set': 'checkins.-1': <entry>


Because it'd overwrite the other elements in the array, and this:



'$set': 'checkins.-1': [...otherEntries, entry]


Would mean inserting the whole array every single time, and since it's very big array, it'd be way too expensive



This, as suggested, looks promising, though:




$each: [ entry ],
$position: -1



But this just lets me push into the checkins array, if I wanted to add an element to the activities subarray it'd involve inserting the whole array, just like the first solution. In this case, I'm looking for something like this:




$each: '$push': 'activities': entry ,
$position: -1



But of course it does not work, mongo complains that




The argument to $each in $push must be an array but it was of type: object




How do I push into the last element, i.e. into checkins.-1.activities?










share|improve this question
















My document looks like this:




_id: ...
checkins: [
...
...

...
activities: [
...
...
]

]



If I want to push into the first element of the checkins array I could do this:



mycol.updateOne(
'_id': ...,
'$push':
'checkins.0.activities': entry
)


But I need to insert entry into the last index of the checkins array, so how could I implement something like this?



mycol.updateOne(
'_id': ...,
'$push':
'checkins.-1.activities': entry
)


Edit:



I can't just do:



'$set': 'checkins.-1': <entry>


Because it'd overwrite the other elements in the array, and this:



'$set': 'checkins.-1': [...otherEntries, entry]


Would mean inserting the whole array every single time, and since it's very big array, it'd be way too expensive



This, as suggested, looks promising, though:




$each: [ entry ],
$position: -1



But this just lets me push into the checkins array, if I wanted to add an element to the activities subarray it'd involve inserting the whole array, just like the first solution. In this case, I'm looking for something like this:




$each: '$push': 'activities': entry ,
$position: -1



But of course it does not work, mongo complains that




The argument to $each in $push must be an array but it was of type: object




How do I push into the last element, i.e. into checkins.-1.activities?







mongodb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 17:20







arielnmz

















asked Mar 23 at 18:41









arielnmzarielnmz

3,63031939




3,63031939












  • Possible duplicate of Can you have mongo $push prepend instead of append?

    – manish kumar
    Mar 23 at 18:44











  • Not quite, the syntax doesn't work the same when dealing with nested arrays. Also, I'd rather not $set the entire activities array every time I need to push one activity

    – arielnmz
    Mar 23 at 18:50







  • 1





    Nested arrays are really bad. Basically impossible to atomically update before relatively recent changes for MongoDB, and mostly they just don't deliver as a structure what you "think" you designed them for. Typically people think "like a relation", but it's not actually how you do it. The -1 problem has long be dealt with by basically "reversing the order of the array", which is the real use case of $position and $sort modifiers. You would be better of actually showing you full use case, since the solution would certainly be to "flatten" the arrays and reverse the order.

    – Neil Lunn
    Mar 23 at 23:54






  • 1





    In other words "show what actually needs to get done" rather than discuss how you think it needs to be done. And be prepared to make structural changes to data.

    – Neil Lunn
    Mar 23 at 23:56











  • show what actually needs to get done Its literally in the two examples. I had to add why they don't work because the site prompted me to add why the suspected dupe isn't a dupe. Typically people think "like a relation" that's totally not what I think, please if you need more info ask for it but don't start a discussion. The -1 problem has long be dealt with by basically "reversing the order of the array" So, how does one reverse the order of an embedded array within another array? Or use $position and $sort within arrays?

    – arielnmz
    Mar 25 at 17:02


















  • Possible duplicate of Can you have mongo $push prepend instead of append?

    – manish kumar
    Mar 23 at 18:44











  • Not quite, the syntax doesn't work the same when dealing with nested arrays. Also, I'd rather not $set the entire activities array every time I need to push one activity

    – arielnmz
    Mar 23 at 18:50







  • 1





    Nested arrays are really bad. Basically impossible to atomically update before relatively recent changes for MongoDB, and mostly they just don't deliver as a structure what you "think" you designed them for. Typically people think "like a relation", but it's not actually how you do it. The -1 problem has long be dealt with by basically "reversing the order of the array", which is the real use case of $position and $sort modifiers. You would be better of actually showing you full use case, since the solution would certainly be to "flatten" the arrays and reverse the order.

    – Neil Lunn
    Mar 23 at 23:54






  • 1





    In other words "show what actually needs to get done" rather than discuss how you think it needs to be done. And be prepared to make structural changes to data.

    – Neil Lunn
    Mar 23 at 23:56











  • show what actually needs to get done Its literally in the two examples. I had to add why they don't work because the site prompted me to add why the suspected dupe isn't a dupe. Typically people think "like a relation" that's totally not what I think, please if you need more info ask for it but don't start a discussion. The -1 problem has long be dealt with by basically "reversing the order of the array" So, how does one reverse the order of an embedded array within another array? Or use $position and $sort within arrays?

    – arielnmz
    Mar 25 at 17:02

















Possible duplicate of Can you have mongo $push prepend instead of append?

– manish kumar
Mar 23 at 18:44





Possible duplicate of Can you have mongo $push prepend instead of append?

– manish kumar
Mar 23 at 18:44













Not quite, the syntax doesn't work the same when dealing with nested arrays. Also, I'd rather not $set the entire activities array every time I need to push one activity

– arielnmz
Mar 23 at 18:50






Not quite, the syntax doesn't work the same when dealing with nested arrays. Also, I'd rather not $set the entire activities array every time I need to push one activity

– arielnmz
Mar 23 at 18:50





1




1





Nested arrays are really bad. Basically impossible to atomically update before relatively recent changes for MongoDB, and mostly they just don't deliver as a structure what you "think" you designed them for. Typically people think "like a relation", but it's not actually how you do it. The -1 problem has long be dealt with by basically "reversing the order of the array", which is the real use case of $position and $sort modifiers. You would be better of actually showing you full use case, since the solution would certainly be to "flatten" the arrays and reverse the order.

– Neil Lunn
Mar 23 at 23:54





Nested arrays are really bad. Basically impossible to atomically update before relatively recent changes for MongoDB, and mostly they just don't deliver as a structure what you "think" you designed them for. Typically people think "like a relation", but it's not actually how you do it. The -1 problem has long be dealt with by basically "reversing the order of the array", which is the real use case of $position and $sort modifiers. You would be better of actually showing you full use case, since the solution would certainly be to "flatten" the arrays and reverse the order.

– Neil Lunn
Mar 23 at 23:54




1




1





In other words "show what actually needs to get done" rather than discuss how you think it needs to be done. And be prepared to make structural changes to data.

– Neil Lunn
Mar 23 at 23:56





In other words "show what actually needs to get done" rather than discuss how you think it needs to be done. And be prepared to make structural changes to data.

– Neil Lunn
Mar 23 at 23:56













show what actually needs to get done Its literally in the two examples. I had to add why they don't work because the site prompted me to add why the suspected dupe isn't a dupe. Typically people think "like a relation" that's totally not what I think, please if you need more info ask for it but don't start a discussion. The -1 problem has long be dealt with by basically "reversing the order of the array" So, how does one reverse the order of an embedded array within another array? Or use $position and $sort within arrays?

– arielnmz
Mar 25 at 17:02






show what actually needs to get done Its literally in the two examples. I had to add why they don't work because the site prompted me to add why the suspected dupe isn't a dupe. Typically people think "like a relation" that's totally not what I think, please if you need more info ask for it but don't start a discussion. The -1 problem has long be dealt with by basically "reversing the order of the array" So, how does one reverse the order of an embedded array within another array? Or use $position and $sort within arrays?

– arielnmz
Mar 25 at 17:02













0






active

oldest

votes












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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317172%2fpush-to-last-element-of-sub-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317172%2fpush-to-last-element-of-sub-array%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript