- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个帖子,我想将其字段 postStatus
设置为 true false 或 null。我想知道 mongo 是否能够索引空值。
此外,如果我选择执行 null、true 和 false 与使用 -1,0,1(或任何 3 个整数)有什么不同吗?
谢谢
最佳答案
是的,null
值被索引了,如果您尝试将第二个 null
值添加到唯一索引中,就可以证明这一点:
connecting to: test
> db.idxtest.createIndex({a:1},{unique:true})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.idxtest.insert({a:"foo"})
WriteResult({ "nInserted" : 1 })
> db.idxtest.insert({b:"bar"})
WriteResult({ "nInserted" : 1 })
> db.idxtest.insert({c:"baz"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error index: test.idxtest.$a_1 dup key: { : null }"
}
})
证明 null 值很重要的另一种方法是对文档包含 null
值的索引字段进行排序:
> db.idxtest2.createIndex({a:1})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.idxtest2.insert({a:1})
WriteResult({ "nInserted" : 1 })
> db.idxtest.insert({a:2})
WriteResult({ "nInserted" : 1 })
> db.idxtest2.insert({b:2})
WriteResult({ "nInserted" : 1 })
> db.idxtest2.insert({a:null})
WriteResult({ "nInserted" : 1 })
> db.idxtest2.find().sort({a:1})
{ "_id" : ObjectId("56786a93ada44c7ffcd38f9f"), "b" : 2 }
{ "_id" : ObjectId("56786aa1ada44c7ffcd38fa0"), "a" : null }
{ "_id" : ObjectId("56786a65ada44c7ffcd38f9d"), "a" : 1 }
> db.idxtest2.find().sort({a:-1})
{ "_id" : ObjectId("56786a65ada44c7ffcd38f9d"), "a" : 1 }
{ "_id" : ObjectId("56786aa1ada44c7ffcd38fa0"), "a" : null }
{ "_id" : ObjectId("56786a93ada44c7ffcd38f9f"), "b" : 2 }
请注意,隐式 null
值具有优先权;可以说,它们是“更多”null
,因为它们甚至没有相应的 key 。
根据 BSON specification , bool 值是一个字节,而整数至少是一个占用 4 个字节的 32 位整数。所以它确实有所作为,如果您有数百万个条目,那么总计... ...好吧,几兆字节。 ;) 所以对于所有实际目的来说,没有区别。
关于mongodb - mongodb 是否能够索引空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401915/
我正在使用 Java 编写一个时钟程序,该程序能够“滴答作响”,但它存在问题。我认为它与 getter 和 setter 或 toString() 方法有关。 计数器类 package clock;
const Index = () => { // Ref Links const frefLinks = { 1: useRef(1), 2: useRef(2), 3: useRef(3
所以我读了here不能 pickle 装饰函数。确实: import multiprocessing as mp def deco(f): def wrapper(*args, **kwarg
我在go1.11.2 linux/amd64 版本。当包godog使用 go get github.com/DATA-DOG/godog/ 安装,godog 可执行文件在 $GOPATH/bin/中创
如何正确压缩字符串,以便 PHP 能够解压缩? 我试过这个: public static byte[] compress(String string) throws IOException {
我们这里的问题是表明 在测试中使用 Kleene 代数。 在 b 的值由 p 保留的情况下,我们有交换条件 bp = pb;两个程序之间的等价性简化为等式 在 b 的值不被 p 保留的情况下,我们有交
我有一个与我的网络相关的非常奇怪的问题,我在具有多个接口(interface)的 VirtualBox 上安装了 RDO Grizzly OpenStack。 虚拟盒子: eth0 - managem
我正在尝试使用 Passport.js授权谷歌OAuth2在 Node.js .我整个星期都在尝试让它工作,但不知道为什么它不工作,所以现在我求助于 stack 寻求一些潜在的帮助。我已经尝试了所有在
我是一名优秀的程序员,十分优秀!