0

I have a collection which have some documents already in it. Now, when i try to insert a new document from PHP using an associated array, the new document is getting inserted in between already existing documents. Why it is not getting inserted at the end of already existing documents. Can anyone provide the answer?

My PHP code snippet for inserting into mongoDB:

$summaryFields = array("Acquisition" => array(
"Something_1" => array("Premium" => "1","Non-Premium" => "2"),
"Something_2" => array("Premium" => "3","Non-Premium" => "4"),
"Something_3" => array("Premium" => "5","Non-Premium" => "6"),
"Something_4" => array("Premium" => "7","Non-Premium" => "8"),
"Something_5" => array("Premium" => "9","Non-Premium" => "10")
)); 
$mongoConn->insert("my_collection", $summaryFields);
  • Can you give an example of what new data is to be inserted and where it is being inserted into the array. –  May 18 '15 at 09:24
  • Actually, it is inserted at the end only. But, when I use db.my_collection.find({}), it is showing the newly inserted documents in between the old documents. I guess there is no problem in insertion. The problem is of showing the documents from the collection in wrong order. – Raju Ponnala May 18 '15 at 09:29
  • Could mongoDB be doing the sorting? –  May 18 '15 at 09:31
  • @jeff : that is what I also want to know.! – Raju Ponnala May 18 '15 at 10:20
  • This is an expected outcome: MongoDB will reuse space from deleted/moved documents, and there is no guaranteed sort order unless you ask for one in your query. – Stennie May 26 '15 at 21:35

0 Answers0