mongodb - Wie füge ich Dokumente zu Array-Eigenschaften in Mongoose hinzu oder entferne sie?
伊谢尔伦
伊谢尔伦 2017-05-17 10:03:17
0
1
642

Wie im Beispiel unten: Was soll ich tun, wenn ich Dokumente zum Listenarray hinzufügen oder daraus löschen möchte? Danke!

{
    "_id" : ObjectId("590a77315a7ae88824b296cf"),
    "user" : "yejia@qq.com",
    "password" : "5475442343",
    "lists" : [
        {
            "create_at" : "1234",
            "update_at" : "1234",
            "title" : "yejia",
            "list_id" : 123
        }
    ]
}
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

Antworte allen(1)
黄舟

pull和push,和MongoDB Node.JS Driver一样的。

参考下面的用法:

Model.update({ }, {’$pull’:{ } } );

Model.update({ }, {’$push’:{ } } );

供参考

Love MongoDB! Have Fun!


Mongoose的pull和push的语法参考如后:

var schema = new mongoose.Schema({ user: 'string', password: 'string', lists: [ {create_at: 'string', update_at: 'string', title: 'string', list_id: 'number'}]});

var User = mongoose.model('User', schema,'test');

//删除
User.update({"user" : "yejia@qq.com"}, 
{'$pull':{ lists : { list_id : 123 }}}, function(err, data){
    if(err) {
        console.log(err);}
    console.log(data);
    });

//添加
User.update({"user" : "yejia@qq.com"}, 
{'$push':{ lists : { create_at: '111', update_at: '222', title: 'test', list_id: 8888}}}, function(err, data){
    if(err) {
        console.log(err);}
    console.log(data);
    });

//打印结果
User.find({},function(err,result){
    console.log(result);
});
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!