how to implement FIFO stack ?

243 views
Skip to first unread message

terbooter

unread,
Mar 15, 2012, 11:38:06 AM3/15/12
to mongodb-user
I want to make FIFO stack for messages. To keep last N messages only.
First idea was make empty array of size N
and add new message like this

'$push' => array('outbox' => $message),
'$pop' => array('outbox' => -1)

But such queue dosnt work.

A. Jesse Jiryu Davis

unread,
Mar 15, 2012, 11:51:06 AM3/15/12
to mongod...@googlegroups.com
Hi, when you say it doesn't work, what behavior are you seeing? On the shell, push / pop syntax is like:

> db.foo.insert({'a':[]})
> db.foo.update({}, {$push:{'a':1}})
> db.foo.find()
{ "_id" : ObjectId("4f620f42781a45d79b8c4ff1"), "a" : [ 1 ] }
> db.foo.update({}, {$push:{'a':2}})
> db.foo.find()
{ "_id" : ObjectId("4f620f42781a45d79b8c4ff1"), "a" : [ 1, 2 ] }
> db.foo.update({}, {$pop:{'a':-1}})
> db.foo.find()
{ "_id" : ObjectId("4f620f42781a45d79b8c4ff1"), "a" : [ 2 ] }

mamu

unread,
Apr 30, 2012, 3:56:54 PM4/30/12
to mongod...@googlegroups.com
You are looking for capped collections. use document insert remove instead of push pop, push pop is for a document only.



On Thursday, March 15, 2012 11:38:06 AM UTC-4, terbooter wrote:
Reply all
Reply to author
Forward
0 new messages