如何在mongo中添加新对象到现有数据

时间:2017-12-19 06:02:26

标签: database mongodb nosql insert-update findandmodify

我的表 UserCollection

上有这样的文档
{
  "FirstName": "Mohammad",
  "LastName": "Waheed", 
}

我想添加另一个数组对象,我该怎么做。?

  "PhoneNumbers":[  //this object is not present in the table
        {
          "number": NumberInt(8332045764) 
        },
        {
          "number": NumberInt(1234567890) 
        } 
     ] 

我的预期结果是这样的:

{
  "FirstName": "Mohammad",
  "LastName": "Waheed",
  "PhoneNumbers":[  
        {
          "number": NumberInt(8332045764) 
        },
        {
          "number": NumberInt(1234567890) //when i call this method again it should store like this
        } 
     ] 
}

这是我尝试过的: 1.只获得单一记录 2。 $ number 包含电话号码

的数据
return $db->update(array('$set' => array("PhoneNumbers.$.number" => $number))); 

1 个答案:

答案 0 :(得分:3)

您可以使用$push命令将多个号码添加到列表

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendPushNotification = functions.database.ref('/Users/{id}').onWrite(event => {
const payload = {
    notification: {
        title: 'Test message',
        body: 'Hope you are fine',
        badge: '1',
        sound: 'default',
    }
};

return admin.database().ref('fcmToken').once('value').then(allToken => {
    if(allToken.val()){
        const token = Object.keys(allToken.val());
        return admin.messaging().sendToDevice(token, payload).then(response => {

        });
    }
});
});