我正在使用此驱动程序https://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/
MongoCollection<Document> collection = database.getCollection("blogs");
String text = "hey";
List<String> searched = new ArrayList<String>();
// MongoCursor<Document> cursor = collection.find("posts").sort(ascendingorder).iterator()
博客集合具有1个变量的json
{
"_id": "randomid"
"posts": "hey my name is bob"
},
{
"_id": "randomid"
"posts": "a hey my name is bob"
},
如果要在此示例中找到搜索到的文本的“嘿”,我想按升序将每个json排序到一个列表中。所有帖子按升序包含“嘿”
例如,上面的列表将是
searched = [
{
"_id": "randomid"
"posts": "a hey my name is bob"
},
{
"_id": "randomid"
"posts": "hey my name is bob"
}
]
答案 0 :(得分:0)
您可以尝试这样查询:
db.blogs.find({"posts": /.*hey.*/}).sort( { posts: -1 } )
或
db.blogs.find({'posts': {'$regex': 'hey', '$options': 'i'}}).sort({ posts: -1 })
答案 1 :(得分:0)
随机ID将是mongodb创建的时间戳。您可以使用它进行排序。
<div class="col-md-6 form-group">
<label for="reg_city" class="control-label">City <span style="color:blue;"><b>*</b></span></label>
<input name="reg_city" type="text" class="form-control" required id="js_reg_city" placeholder="">
</div>