如何在PHP Mongo中查询空字符串?

如何在PHP Mongo中查询空字符串?,php,mongodb,Php,Mongodb,我想查询集合中client字段为空字符串的所有文档。我看过很多关于如何检查某个内容是否为空字符串的示例,但我找不到任何相反的示例。我尝试了db.collection.find({client:}),但我得到了一个空集 编辑:集合中的示例条目如下所示 { "_id" : ObjectId("54eb59699e12a795078b80da"), "reportID" : "1472031", "orgID" : "336", "client" : "", "customerID" : Number

我想查询集合中
client
字段为空字符串的所有文档。我看过很多关于如何检查某个内容是否为空字符串的示例,但我找不到任何相反的示例。我尝试了
db.collection.find({client:})
,但我得到了一个空集

编辑:集合中的示例条目如下所示

{ "_id" : ObjectId("54eb59699e12a795078b80da"), "reportID" : "1472031", "orgID" : "336", "client" : "", "customerID" : NumberLong(1), "address" : "123 main st", "city" : "Grove City", "state" : "OH", "zip" : "43123", "county" : "Franklin", "gpsLatitude" : null, "gpsLongitude" : null, "dateDue" : ISODate("2012-08-18T00:00:00Z"), "dateDueClient" : ISODate("2012-08-18T00:00:00Z"), "dateComplete" : ISODate("2012-08-18T00:00:00Z"), "dateCompleteEstimate" : NumberLong(0), "contractorSubmissionDate" : ISODate("2012-08-19T00:26:23Z"), "createdOn" : ISODate("2012-08-19T00:21:37Z"), "assignedToContractorOn" : ISODate("2012-08-19T00:21:37Z"), "workTypeID" : "6338", "assignedAdmin" : "7880", "contractorID" : "7880", "categoryID" : "0", "historyLength" : NumberLong(0), "invoiceDate" : ISODate("2012-08-18T00:00:00Z"), "submittedToClient" : ISODate("2012-08-18T00:00:00Z"), "paymentContractor" : NumberLong(0), "paymentClient" : NumberLong(0), "contractorIsPaid" : NumberLong(0), "clientIsPaid" : NumberLong(0), "sentinel" : NumberLong(1), "isFrozen" : NumberLong(0), "numTimesClientReady" : "1", "pcrResponses" : [ ] }

有很多字段,但是客户端的字段非常接近开头。

您发布的查询应该可以工作。为了验证,我将该数据作为一行插入到测试集合中,并立即返回,返回时带有
db.test.find({client:'})

我想你可能在别的地方有问题。在运行查询时,我曾经有一两次在错误的数据库中,或者在集合名称中有一个输入错误。为了验证数据的外观,我通常会:

> db.tset.find({client: ''});
[] // What? No results? ...backspacebackspacebackspace

> db.tset.find();
[] // What? No results at all for no query? I must be in the.. OH!

> db.test.find();
[] // What? I thought I had a typo in my collection name. What database am I in?

> db
tseting // OH! I keep making that typo..

> use testing
switched to db testing

> db.test.find({client: ''});
[] // What? Still no results? This is weird...

> db.test.insert({client: ''});

> db.test.find({client: ''});
{ "_id" : ObjectId("54f1b5e5d05052fce4fb6684"), "client" : "" }
// Hmm, ok, so there's nothing wrong with the query. The data came up
// So I'm still just in the wrong place? Really?
// Maybe I woke up on the wrong side of the bed today. I should eat lunch.

有时,隧道视野会让你认为问题出在它不存在的地方。如果这有帮助或没有帮助,请留下评论,也许我们可以想出更多的故障排除方法

这应该行得通。你能给我们看一个你想匹配但未能匹配的示例文档吗?这是一个语法错误,因此文档的格式与语法错误无关。你在原始问题中说这是一个空结果,而不是语法错误。如果是语法错误,请发布代码和错误。对
{“client”:“}
的查询可以很好地处理该文档。不过,它是用mongoshell语法编写的。是PHP代码不起作用吗?你能给我们看一下PHP代码吗?很抱歉模棱两可。我从
db.collection.find({“client”:“”})
中得到一个空结果,并且从
db.collection.find({“client”:“{$in':{”,array(),null,0}}}})中得到一个语法错误。
。我还没有打算把它放到PHP代码中。我仍在努力让它在mongo Shells中发挥作用。这与错误的收藏或任何打字错误无关,但我认为我关于空条目外观的文档可能是错误的。我不认为这是一个空字符串,可以执行
var x=db.collection.findOne({u id:ObjectId(“54eb59699e12a795078b80da”)
然后
typeof x.client
,查看它返回什么?