问题:
我有一个非常大的集合,按字段索引ts:(时间戳)
> db.events.ensureIndex({'ts': -1})
Run Code Online (Sandbox Code Playgroud)
我想获得最后5个条目.让我感到惊讶的是,查询不使用索引,因此非常慢:
> db.events.find().sort({'ts': -1, '_id': -1}).limit(5)
Run Code Online (Sandbox Code Playgroud)
但是,仅按ts或其他字段排序使用索引:
> db.events.find().sort({'ts': -1}).limit(5)
> db.events.find().sort({'_id': -1}).limit(5)
Run Code Online (Sandbox Code Playgroud)
这是MongoDB中的一个错误,这确实是一个记录的功能还是我做错了什么?
附加信息:
> db.events.find().sort({'ts': -1, '_id': -1}).limit(5).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 795609,
"nscannedObjects" : 795609,
"n" : 5,
"scanAndOrder" : true,
"millis" : 22866,
"nYields" : 73,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
}
}
> db.events.find().sort({'ts': -1}).limit(5).explain()
{
"cursor" : "BtreeCursor ts_-1",
"nscanned" : 5,
"nscannedObjects" : 5,
"n" …Run Code Online (Sandbox Code Playgroud) 当我尝试从PHP向MongoDB 插入空关联数组(hashmap/ dictionary/ dict/ {})时,它总是作为非关联空数组(list/ [])插入.我可以强制关联数组吗?
例:
$m = new Mongo('mongodb://localhost:27017');
$db = $m->selectDB('test');
$collection = $db->selectCollection('test');
// 1: inserts []
$d = array( 'x' => array() );
$collection->insert($d);
// 2: inserts []
$d = array( 'y' => array('a'=>'123') );
unset($d['y']['a']);
$collection->insert($d);
// 3: inserts {}
$d = array( 'z' => array('a'=>'123') );
$collection->insert($d);
$collection->update(array('_id' => $d['_id']), array('$unset' => array('z.a'=>true)));
Run Code Online (Sandbox Code Playgroud)
结果如下:
> db.test.find().pretty()
{ "_id" : ObjectId("510fb9ede695c5381a000000"), "x" : [ …Run Code Online (Sandbox Code Playgroud) 免责声明:关于SO有许多类似的问题,但我正在寻找一个实用的建议,而不仅仅是一般原则.另外,请随意指出"理想"算法的实现(PHP会很好;)但请提供具体信息(如何工作).
计算用于存储在数据库中的密码的哈希字符串的最佳方法是什么?我知道我应该:
我在考虑使用这样的算法:
x = md5( salt + password);
repeat N-times:
x = md5( salt + password + x );
Run Code Online (Sandbox Code Playgroud)
我确信这是非常安全的,但有几个问题浮现在脑海中:
在盐中包含用户名是否有益?
我决定为所有用户使用普通盐,这有什么缺点?
建议的最小盐长度是多少?
我应该使用md5,sha还是其他什么?
上述算法/任何建议有什么问题吗?
...(随意提供更多:)
我知道决定必然取决于具体情况,但我正在寻找一种解决方案:
那么,理想算法会是什么样子,最好是伪代码?
我在PostgreSQL中有两个表。第一个应该具有一个自动递增的ID字段,第二个要引用该字段:
CREATE TABLE tableA (id SERIAL NOT NULL PRIMARY KEY, ...)
CREATE TABLE tableB (parent INTEGER NOT NULL REFERENCES tableA(id), ...)
Run Code Online (Sandbox Code Playgroud)
根据文档,SERIAL在签名时充当无符号4字节整数INTEGER:
serial 4 bytes autoincrementing integer 1 to 2147483647
integer 4 bytes typical choice for integer -2147483648 to +2147483647
Run Code Online (Sandbox Code Playgroud)
如果我正确理解,我使用的数据类型不兼容,但是PostgreSQL显然缺少无符号整数。我知道我可能不会使用超过2 * 10 ^ 9个ID(如果我这样做了,我总是可以使用BIGSERIAL),这并不是那么重要,但是对我来说,带符号整数引用一个无符号似乎有点不干净一。我确信必须有一个更好的方法-我错过了什么吗?
我想从跟踪单独的触摸顺序touchesBegan通过touchesMoved,直到touchesEnded.我正在获取单点触摸事件的坐标,但我想知道哪个触摸事件对应于哪个触摸事件序列.
例如,如果我在屏幕上移动第一根手指,然后用第二根手指触摸屏幕,并移除第一根手指 - 我想显示第一根手指的红色坐标和第二根手指的坐标蓝色.
这可能吗?如果是,我如何确定哪些事件应为"红色"以及哪些事件应为"蓝色"?
这是我的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleTouches:[event allTouches]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleTouches:[event allTouches]];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleTouches:[event allTouches]];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleTouches:[event allTouches]];
}
- (BOOL)handleTouches: (NSSet*)touches {
for (UITouch* touch in touches) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud) 我试图在SVG中定义自己的命名空间,所以我可以使用自己的标签和属性.如果我理解正确,SVG应如下所示:
<svg xmlns:myns="http://www.example.com/whatever/">
<g myns:mycustomattr="123">
...
Run Code Online (Sandbox Code Playgroud)
我通过D3添加根SVG元素,但添加了一个名称空间attr失败:
var svg = d3.select("#container")
.append("svg")
.attr('xmlns:myns', 'http://www.example.com/whatever/')
Run Code Online (Sandbox Code Playgroud)
上面的代码导致:NamespaceError:尝试以对名称空间不正确的方式创建或更改对象
使用D3.js将名称空间添加到SVG的正确方法是什么?