您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

$push to sorted array

2025/12/9 6:42:04发布16次查看
by sam weaver, mongodb solutions architect and alberto lerner, mongodb kernel lead mongodb 2.4 introduced a feature that many have requested for some time - the ability to create a capped array. capped arrays are great for any application
by sam weaver, mongodb solutions architect and alberto lerner, mongodb kernel lead
mongodb 2.4 introduced a feature that many have requested for some time - the ability to create a capped array.
capped arrays are great for any application that needs a fixed size list. for example, if you’re designing an ecommerce application with mongodb and want to include a listing of the last 5 products viewed, you previously had to issue a $push request for each new item viewed, and then a $pop to kick the oldest item out of the array. whilst this method was effective, it wasn’t necessarily efficient. let’s take an example of the old way to do this:
first we would need to create a document to represent a user which contains an array to hold the last products viewed:
db.products.insert({last_viewed:[bike,cd,game,bike,book]})db.products.findone(){ _id : objectid(51ff97d233c4f2089347cab6), last_viewed : [ bike, cd, game, bike, book ]}
we can see the user has looked at a bike, cd, game, bike and book. now if they look at a pair of ski’s we need to push ski’s into the array:
db.products.update({},{$push: {last_viewed: skis}})db.products.findone(){ _id : objectid(51ff97d233c4f2089347cab6), last_viewed : [ bike, cd, game, bike, book, skis ]}
you can see at this point we have 6 values in the array. now we would need a separate operation to pop “bike” out:
db.products.update({},{$pop: {last_viewed: -1}})db.products.findone(){ _id : objectid(51ff97d233c4f2089347cab6), last_viewed : [ cd, game, bike, book, skis ]}
in mongodb 2.4, we combined these two operations to maintain a limit for arrays sorted by a specific field.
using the same example document above, it is now possible to do a fixed sized array in a single update operation by using $slice:
db.products.update({},{$push:{last_viewed:{$each:[skis],$slice:-5}}})
you push the value ski’s into the last_viewed array and then slice it to 5 elements. this gives us:
db.products.findone(){ _id : objectid(51ff9a2d33c4f2089347cab7), last_viewed : [ cd, game, bike, book, skis ]}
mongo maintains the array in natural order and trims the array to 5 elements. it is possible to specify to slice from the start of the array or the end of the array by using positive or negative integers with $slice. it is also possible to sort ascending or descending by passing $sort also. this helps avoid unbounded document growth, and allows for the event system to guarantee in-order delivery.
there are lots of other applications for this feature:
keeping track of the newest messages in a messaging systemmanaging recent clicks on a website last accessed/viewed productstop x users/comments/poststhe list goes on.
this feature is available in mongodb 2.4, but there are many extensions requested, such as full $sort and $slice semantics in $push (server-8069), and making the $slice operation optional (server-8746). both of these are planned for the 2.5 development series.
special thanks to yuri finkelstein from ebay who was very enthusiastic about this feature and inspired this blog post.
原文地址:$push to sorted array, 感谢原作者分享。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product