我一直在玩mongodb中的存储推文,每个对象看起来像这样:
{
"_id" : ObjectId("4c02c58de500fe1be1000005"),
"contributors" : null,
"text" : "Hello world",
"user" : {
"following" : null,
"followers_count" : 5,
"utc_offset" : null,
"location" : "",
"profile_text_color" : "000000",
"friends_count" : 11,
"profile_link_color" : "0000ff",
"verified" : false,
"protected" : false,
"url" : null,
"contributors_enabled" : false,
"created_at" : "Sun May 30 18:47:06 +0000 2010",
"geo_enabled" : false,
"profile_sidebar_border_color" : "87bc44",
"statuses_count" : 13,
"favourites_count" : 0,
"description" : "",
"notifications" : null,
"profile_background_tile" : false,
"lang" : "en", …Run Code Online (Sandbox Code Playgroud) 是否可以查询特定日期?
我在mongo Cookbook中发现我们可以在一个范围内查询日期范围 ,如下所示:
db.posts.find({"created_on": {"$gte": start, "$lt": end}})
Run Code Online (Sandbox Code Playgroud)
但具体日期是否可能?这不起作用:
db.posts.find({"created_on": new Date(2012, 7, 14) })
Run Code Online (Sandbox Code Playgroud) 我的问题:给我一份超过X时间的文件清单.
如果我创建了一个文档:
db.dates.insert({date: new Date()});
现在我只想在"约会"变成30分钟时找到它:
db.dates.find({ $where: "this.date.getTime() + 30 * 60000 <= new Date()"});
这是有效的,但在Mongo文档中明确指出,对于查询的$,存在显着的性能损失.
因此,问题是,还有更好的方法吗?
==========更新1 ==========
我应该补充一点,我希望这个查询功能"动态地"创建一次查询并使用它来获取有限集合上的可用光标...而且我不确定它是否真的可行.
我会测试并重新发布.
==========更新2 ==========
因此,看起来我的"延迟"队列将不得不在代码中处理,无论是使用轮询还是一些"检查,然后睡眠"算法,因为这似乎是mongo的延迟复制正在进行的操作(来自db.cpp):
if ( replSettings.slavedelay && ( unsigned( time( 0 ) ) < nextOpTime.getSecs() + replSettings.slavedelay ) ) {
assert( justOne );
oplogReader.putBack( op );
_sleepAdviceTime = nextOpTime.getSecs() + replSettings.slavedelay + 1;
dblock lk;
if ( n > 0 ) {
syncedTo = last;
save();
}
log() << "repl: applied " << n << " operations" << …Run Code Online (Sandbox Code Playgroud)