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

时间:2015-02-25 19:06:38

标签: php mongodb

我想查询我的收藏中字段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" : [ ] }

有很多领域,但客户端非常接近开始。

1 个答案:

答案 0 :(得分:0)

您发布的查询应该有效。只是为了验证,我将这些数据作为一行插入到测试集合中,然后使用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.

有时隧道视觉会让你认为问题出在某个地方,而不是它。如果这有用或者没有帮助,请发表评论,也许我们可以提出一些更多的故障排除想法。