I am trying to implement a simple message queue in MongoDB. But I can not find findAndModify to work with cl-mongo.
In the mongo shell, I can do this (reformatted for readability):
> db.queue2.insert(
{
"data": "test",
"date_created": new Date(),
"date_updated": null,
"date_completed": null,
"state": 0
})
WriteResult({ "nInserted" : 1 })
> db.queue2.findAndModify(
{
query: {"state": 0},
update: {
$inc: {"state": 1},
$currentDate: {"date_updated": false}
}
})
{
"_id" : ObjectId("56e7142c3ade7afa5209bf0f"),
"data" : "test",
"date_created" : ISODate("2016-03-14T19:42:36.860Z"),
"date_updated" : null,
"date_completed" : null,
"state" : 0
}
> db.queue2.find()
{
"_id" : ObjectId("56e7142c3ade7afa5209bf0f"),
"data" : "test",
"date_created" : ISODate("2016-03-14T19:42:36.860Z"),
"date_updated" : ISODate("2016-03-14T19:43:10.499Z"),
"date_completed" : null,
"state" : 1
}
I can use findin cl-mongo:
> (pp (db.find "queue2" (kv "query" (kv "state" 0))))
{
"_id" -> objectid(56E858283ADE7AFA5209BF10)
"data" -> test
"date_created" -> CL-MONGO::BSON-TIME Tue Mar 15 2016 19:44:56 (GMT+1)
"date_updated" -> NIL
"date_completed" -> NIL
"state" -> 0.0d0
}
I am trying to run findAndModifyusing db.run-command. Reading the documentation and the cl-mongo code, I think it should be called like this:
> (pp (db.run-command "findAndModify"
:arg (kv (kv "query" (kv "state" 0))
(kv "update" (kv "$inc" (kv "state" 1))))
:collection "queue2"))
{
"ok" -> 0.0d0
"errmsg" -> need remove or update
}
NIL
I tried some options like :findAndModifytrying to pass parameters in a list, etc., but I keep getting the same error.
I should probably mention that I'm new to both Lisp and MongoDB, so if I missed something obvious, I apologize.
How to run findAndModify using cl-mongo?