0

I have a collection Posts and each post has a title and an author.

Posts : {
    id     : 7867887687,
    title  : unnamed-1,
    author : joe 
},{
    id     : 2867887687,
    title  : unnamed-2,
    author : joe 
},{
    id     : 1867337687,
    title  : happyhour,
    author : joe 
},{
    id     : 2367337687,
    title  : crappyhour,
    author : deb 
}

I want to return a cursor with only the posts by joe that have a title unnamed*. The next step is to sort the unnamed posts and add a new unnamed post with the next available -x number. (if one had been previously deleted the next available -x might be in the middle of others ... (sort of like creaiting new folders in windows))

How would I do that in Mongo?

Or should I return all posts by joe and then do a forEach to find the unnamed ones somehow?

Or some other method?

4

1 回答 1

2

使用正则表达式:

db.Posts.find({author:'joe', title:/^unnamed/i})

这里有更多要阅读的内容http://docs.mongodb.org/manual/reference/operator/regex/#op._S_regex

于 2013-04-18T02:00:27.197 回答