2014-01-20 93 views
2

如何使用sails-mongo适配器在sails.js中使用类似“$ inc,$ set,upsert ...”的mongodb操作?

我试过这段代码,但是适配器没有标识这些选项。

Word.update(
    {coincidence: 'aaaaa'}, 
    {amount: 222}, 
    {upsert:true,safe:true}, 
    function(err,data){ 
    if (err){ 
     console.log(err); 
    } else { 
     console.log("score succeded"); 
    } 
    } 
); 
+0

我在最后添加了一个缺少的右括号。 –

回答

3

你必须使用模型的native方法来做到这一点。它返回本地蒙戈司机的集合的实例:

Word.native(function(err, collection) { 
    collection.update(
    {coincidence: 'aaaaa'}, 
    {amount: 222}, 
    {upsert:true,safe:true}, 
    function(err){ 
     if (err){ 
     console.log(err); 
     } else { 
     console.log("score succeded"); 
     } 
    } 
); 
}); 

有关蒙戈本地驱动程序的文档,这将告诉你,你可以由native方法返回的集合做什么见here

+0

当使用本地方法时,模型的钩子是否被触发? – Luc

+1

如果您的意思是'.beforeCreate'和'.afterUpdate'等生命周期回调,那么答案是否定的。你可以随时打电话给自己;他们只是像其他任何类的方法(例如'Word.beforeCreate(values,callback)') – sgress454

相关问题