Skip to content

Instantly share code, notes, and snippets.

@geoffeg
Created February 10, 2012 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoffeg/1786051 to your computer and use it in GitHub Desktop.
Save geoffeg/1786051 to your computer and use it in GitHub Desktop.
Final MongoDB fixed-sized array example.
db.searches.remove()
db.searches.save(
{_id: 1,
"searches-size" : 3,
"searches" : [
{"site" : "google", "q": "restaurants"},
{"site" : "yahoo", "q" : "shoes"},
{"site" : "bing", "q" : "cheap flights"}
]
}
)
db.searches.update(
{ _id : 1 },
{ $inc: { "searches-size" : 1 },
$push : { "searches" : { "site" : "blekko.com", "q" : "mongodb" } }
}
);
db.searches.update(
{ _id : 1, "searches-size" : { $gt: MAX-ARRAY-LENGTH } },
{ $inc: { "searches-size" : -1 }, $pop : { "searches" : -1 } }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment