javascript - Pushing a collection to a subdocument by Index? -


i'm running below code, in attempt create subdocument under existing subdocument. basic structure is:

user -> (many comments) -> (many ratings).

the rating object basic javascript object, structure;

rating: {     userid: "userid",     rating: 4 } 

so, comment @ index [0], i'm attempting push new rating following;

db.getcollection('users').update(                 {id: "user123", },                 {                     $push: {                         comments[0].ratings: rating                     }                 }) 

the return mongo console (with rating set test string of "test"), is;

error: line 5: unexpected token [ 

i attempted around wrapping comma's around comments[0];

db.getcollection('insights').update(                     {id: "b5e5bf69-071b-4af2-99b2-5165b47499cb", },                     {                         $push: {                             "comments[0].ratings": "test"                         }                     }) 

this returning success;

updated 1 existing record(s) in 2ms 

however, subdocument not appear @ all.

could point me in right direction on this?

found answer this; array[index] format not work mongo. instead, use object notation. so, instead of;

$push: {"comments[0].ratings": "test"} 

use;

$push: {"comments.0.ratings": "test"} 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -