0
我不確定這是否可行。我有一個books
集合,其數據結構如下。在特定索引中插入mongodb嵌套元素
{
"_id" : ObjectId("6539d19532f73ad544000001"),
"name" : "PHP",
"books" : [
{"author": "jim", "title" : "Book1", "price":"200"},
{"author": "joe", "title" : "Book2", "price":"300"},
{"author": "amy", "title" : "Book3", "price":"500"}
]
}
現在我想插入一個新的對象到書籍部分。該對象類似於{"author": "amy", "title" : "Book4", "price":"400"}
。因爲價格低於500,所以我想在Book3之前插入該對象。所以我的最終對象看起來像下面的某種方式。
{
"_id" : ObjectId("6539d19532f73ad544000001"),
"name" : "PHP",
"books" : [
{"author": "jim", "title" : "Book1", "price":"200"},
{"author": "joe", "title" : "Book2", "price":"300"},
{"author": "amy", "title" : "Book4", "price":"400"}
{"author": "amy", "title" : "Book3", "price":"500"}
]
}
是的,我可以做到這一點。所以沒有辦法在特定位置插入對象。 – deepakb