2016-08-04 56 views
0

我有这样的玛MongoDB中:如何在mongoDB中使用空数组?

"_id" : ObjectId("57a376dc317843700b7bd83f"), 
    "approved" : false, 
    "experience" : 0, 
    "placeOfWork" : "", 
    "education" : "", 
    "price" : 0, 
    "courses" : [], 
    "certificates" : [], 
    "createdAt" : ISODate("2016-08-04T17:09:48.773Z"), 
    "profileImg" : "images/profile.png", 
    "role" : "doctor", 
    "phone" : "", 
    "password" :'', 
    "email" : "[email protected]", 
    "foreName" : "", 
    "surname" : "", 
    "name" : "", 
    "__v" : 0 
} 

,我想存储在字符串数组图像证书[“形象”,“图像” ...]但我nee的特殊订单,是更多钞票像在JavaScript中由索引等

certificates{index] = 'something'; 

添加图片到阵列和mongogoDb它存储这样的证书:[0: '东西',1: '某物' 中,x: '某物'] 0,1中,x感谢阵列证书的索引。

回答

0

您使用$push运算符将元素放入数组中,将元素添加到数组的末尾。要在某个位置插入元素,您可以使用$ push和$position运算符。如果数组为空,则不会创建空元素。为了模拟你需要的行为,你可以像这样{index: 0, image: "something"}将文档添加到数组中。

顺便说一句,如果你要存储图像,你可能需要看看GridFS文档。