我怎样才能让MongoDB评估一些JavaScript以获得一个领域的价值? [英] How can I ask MongoDB to evaluate some JavaScript in order to obtain value for a field?
问题描述
我想让MongoDB动态地为我插入的文档的某个字段赋值。例如:MongoDB服务器的当前时间就像NOW()在MySQL中所做的那样。
我试过这个:
c:= mongoSession.DB(myapp)。C(instances)
rand.Seed(time.Now()。UnixNano())
err:= c.Insert(
struct {Serial,Priority,Url,LastSeen interface {}} {
Url:getInformedHost()+:+ getRunningPortString(),
Priority :rand.Int(),
LastSeen:mongoNow()}
)
checkError(err,无法在MongoDB服务器上注册,3)
$ c
我有这个帮助函数:
pre $ code > func mongoNow()bson.JavaScript {
return bson.JavaScript {Code:
(new Date())。ISODate('YYYY-MM-DD hh:mm:ss')}
}
LastSeen字段以脚本形式存储而不是评估:
[_ id] => MongoId对象(
[$ id] => 502d6f984eaead30a134fa10
)
[priority] => 1694546828
[url] => 127.0.0.1:8080
[lastseen] => MongoCode对象(
[code] =>(new Date())。ISODate('YYYY-MM-DD hh:mm:ss')
[scope] => Array(
)
)
我怎样才能得到一些javascript而不是插入的?
请参阅MongoDB文档中的以下URL: http://www.mongodb.org/display/DOCS/Server -batch +执行#server-sideCodeExecution-Storingfunctionsserverside
有一个特殊的系统集合称为
system.js
可以存储JavaScript函数以供重用。
请注意,支持和服务器端代码的性能(相当于存储过程)仍然有点差(详细信息请参考链接)。
编辑:
使用 mgo驱动程序从Go调用存储过程>使用 mgo.Database
类型的 Run()
方法( direct link ),然后用Javascript代码发出 eval
命令作为参数执行服务器端。例如:
db.Run(bson.M {eval:myStoredFunction();})
代码未经测试 无法在MongoDB I want to let MongoDB dynamically assign a value to one of the fields of the document I'm inserting. For example: the current time from MongoDB server just like NOW() would do in MySQL. I tried this: I have this helper function: the LastSeen field gets stored as a script instead of evaluated: How can I get some javascript evaluated instead of inserted? See the following URL on the MongoDB documentation: There is a special system collection called Note though, that the support and performance of server-sided code (equivalent to stored procedures) is still a little poor (details in link). Edit: To call a stored procedure from Go using the mgo driver use the code untested It is not possible to have code evaluated in a MongoDB 这篇关于我怎样才能让MongoDB评估一些JavaScript以获得一个领域的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
insert
语句中评估代码。c := mongoSession.DB("myapp").C("instances")
rand.Seed(time.Now().UnixNano())
err := c.Insert(
struct{Serial, Priority, Url, LastSeen interface{}}{
Url: getInformedHost() + ":" + getRunningPortString(),
Priority: rand.Int(),
LastSeen: mongoNow() }
)
checkError(err, "Could not register on MongoDB server.", 3)
func mongoNow() bson.JavaScript {
return bson.JavaScript{Code:
"(new Date()).ISODate('YYYY-MM-DD hh:mm:ss')"}
}
[_id] => MongoId Object (
[$id] => 502d6f984eaead30a134fa10
)
[priority] => 1694546828
[url] => 127.0.0.1:8080
[lastseen] => MongoCode Object (
[code] => (new Date()).ISODate('YYYY-MM-DD hh:mm:ss')
[scope] => Array (
)
)
system.js
that can store JavaScript functions to be reused.mgo.Database
type's Run()
method (direct link) and issue an eval
command with the Javascript code to be executed server-side as argument. Something like:db.Run(bson.M{"eval": "myStoredFunction();"})
insert
statement.