如何在 mongodb 中使用 null 参数?
how to use null parameter in mongodb?
我输入了一个参数,然后使用mongodb。
我的查询是
db.collections.find({"tags":parameter});
我想运行查询,当参数为“”(null或“”)时。
会是
db.collections.find({"tags":""});
返回空值
如何在 mongoDB 中使用输入参数 null 或 ""?
编辑
对不起。我是初学者,非常抱歉。
我想在输入 null 时获取所有返回的值
例如我的 collections,
collections
{
"_id": 0,
"tags": ["happy", "sad", "nice", "bad"]
},
{
"_id": 1,
"tags": ["bad", "gloomy"]
}
我想要与下面相同的结果。
> Db.collections.find ({"tags": ""})
{
"_id": 0,
"tags": ["happy", "sad", "nice", "bad"]
},
{
"_id": 1,
"tags": ["bad", "gloomy"]
}
// Return all collections.
> Db.collections.find ({"tags": "happy"})
{
"_id": 0,
"tags": ["happy", "sad", "nice", "bad"]
}
// Return matching collections.
但是,db.collections.find ({"tags": ""}) 带出来的结果是空的。
输入空值时如何打印出所有结果?
由于 null
值可以用多种方式表示,具体取决于首先写入数据库的语言,因此您需要组合使用各种东西。您的查询需要类似于
db.collection.find({$or:[{"tags":{"$type":"null"}}, {"tags": {"$exists":false}}, {"tags":""}]})
由于BSON有Null的概念,所以我们有类型检查,看字段是否存在,只是没有值。除此之外,该字段根本不存在,因此必须明确检查。最后,根据语言和字段序列化的方式,空字符串是可能的。
注意
{"tags":null}
和
{"tags":{"$type":"null"}}
本质上是一样的。
这是一个简单的例子
> db.test.insert({"abc":null})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
> db.test.find({"abc":{$type:10}})
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
> db.test.find({"abc":{$type:"null"}})
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
> db.test.find({"abc":null})
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
db.test.find({$or:[{"tags":{"$type":"null"}}, {"tags": {"$exists":false}}, {"tags":""}]})
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
如您所见,它们都有效,尽管最后一个查询是最彻底的测试方法。
编辑操作已更改的问题
当您键入 null
时,您无法找到所有值。这是一个领域的价值和潜在状态。你需要在这里做一个隐式的 $and
来得到你想要的。
db.collection.find({tags:{$exists:true}, tags:{$in:["happy","sad"]}})
您实际上是如何 assemble 在代码中做到这一点的?好吧,这取决于您的语言,但这里有一些伪代码。
def getTags(myTags):
if (tags is None):
db.collection.find({ tags: { "$exists": true } })
else:
db.collection.find({ tags: { "$exists": true }, tags: {"$in": myTags } })
你也可以通过使用显式 $and
def getTags(myTags):
query = [{ tags: { "$exists": true } }]
if (tags is Not None):
query.add({tags: {"$in": myTags } })
db.collection.find({ "$and": query })
我希望这能更彻底地回答您的问题。
接受的答案不适合这个问题(至少现在是这样)。
您可以使用 $expr 运算符,如下所示:
assuming that your parameter name is tagsParam
and its value may be an Array
or null
db.collections.find({
$or: [
{ $expr: !tagsParam || !tagsParam.length },
{ tags: { $in: paramter } }
]
});
在这种情况下,如果参数值为 ["happy"]
、null
甚至空数组 []
.
,您将获得所需的结果
我输入了一个参数,然后使用mongodb。
我的查询是
db.collections.find({"tags":parameter});
我想运行查询,当参数为“”(null或“”)时。
会是
db.collections.find({"tags":""});
返回空值
如何在 mongoDB 中使用输入参数 null 或 ""?
编辑
对不起。我是初学者,非常抱歉。
我想在输入 null 时获取所有返回的值
例如我的 collections,
collections
{
"_id": 0,
"tags": ["happy", "sad", "nice", "bad"]
},
{
"_id": 1,
"tags": ["bad", "gloomy"]
}
我想要与下面相同的结果。
> Db.collections.find ({"tags": ""})
{
"_id": 0,
"tags": ["happy", "sad", "nice", "bad"]
},
{
"_id": 1,
"tags": ["bad", "gloomy"]
}
// Return all collections.
> Db.collections.find ({"tags": "happy"})
{
"_id": 0,
"tags": ["happy", "sad", "nice", "bad"]
}
// Return matching collections.
但是,db.collections.find ({"tags": ""}) 带出来的结果是空的。
输入空值时如何打印出所有结果?
由于 null
值可以用多种方式表示,具体取决于首先写入数据库的语言,因此您需要组合使用各种东西。您的查询需要类似于
db.collection.find({$or:[{"tags":{"$type":"null"}}, {"tags": {"$exists":false}}, {"tags":""}]})
由于BSON有Null的概念,所以我们有类型检查,看字段是否存在,只是没有值。除此之外,该字段根本不存在,因此必须明确检查。最后,根据语言和字段序列化的方式,空字符串是可能的。
注意
{"tags":null}
和
{"tags":{"$type":"null"}}
本质上是一样的。
这是一个简单的例子
> db.test.insert({"abc":null})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
> db.test.find({"abc":{$type:10}})
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
> db.test.find({"abc":{$type:"null"}})
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
> db.test.find({"abc":null})
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
db.test.find({$or:[{"tags":{"$type":"null"}}, {"tags": {"$exists":false}}, {"tags":""}]})
{ "_id" : ObjectId("56670b3072f096ee05a72063"), "abc" : null }
如您所见,它们都有效,尽管最后一个查询是最彻底的测试方法。
编辑操作已更改的问题
当您键入 null
时,您无法找到所有值。这是一个领域的价值和潜在状态。你需要在这里做一个隐式的 $and
来得到你想要的。
db.collection.find({tags:{$exists:true}, tags:{$in:["happy","sad"]}})
您实际上是如何 assemble 在代码中做到这一点的?好吧,这取决于您的语言,但这里有一些伪代码。
def getTags(myTags):
if (tags is None):
db.collection.find({ tags: { "$exists": true } })
else:
db.collection.find({ tags: { "$exists": true }, tags: {"$in": myTags } })
你也可以通过使用显式 $and
def getTags(myTags):
query = [{ tags: { "$exists": true } }]
if (tags is Not None):
query.add({tags: {"$in": myTags } })
db.collection.find({ "$and": query })
我希望这能更彻底地回答您的问题。
接受的答案不适合这个问题(至少现在是这样)。
您可以使用 $expr 运算符,如下所示:
assuming that your parameter name is
tagsParam
and its value may be anArray
ornull
db.collections.find({
$or: [
{ $expr: !tagsParam || !tagsParam.length },
{ tags: { $in: paramter } }
]
});
在这种情况下,如果参数值为 ["happy"]
、null
甚至空数组 []
.