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

我想查询我的集合中 client </ code>字段的所有文档 一个空字符串。 我已经看过很多关于如何检查某些东西是不是空字符串的例子,但我找不到任何相反的例子。 我已经尝试了 db.collection.find({client:“”})</ code>但我得到了一个空集。</ p>

编辑:示例中的条目 集合看起来像</ p>

  {“_ 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”),“  submittedToClien  t“: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”:[]} 
</ code> </ pre>
\ n

有很多字段,但客户端非常接近开头。</ p>
</ div>

展开原文

原文

I'd like to query for all documents in my collection where the field client is an empty string. I've seen plenty of examples of how to check if something's not an empty string but I can't find any examples of how to do the opposite. I've tried db.collection.find({client: ""}) but I get back an empty set.

EDIT: The example entry in the collection looks like

{ "_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" : [ ] }

There's a lot of fields but the client one is pretty close to the beginning.

dongqiongjiong4740
dongqiongjiong4740 抱歉的含糊不清。我从db.collection.find({“client”:“”}得到一个空结果,我从db.collection.find({'client':{'$in':{'',数组中得到一个语法错误(),NULL,0}}})。我还没把它放到PHP代码中。我仍然只是想让它在mongoshell中运行
5 年多之前 回复
draj30958
draj30958 你说在原始问题中它是一个空结果,而不是语法错误。如果是语法错误,请发布代码和错误。{“client”:“”}上的查询适用于该文档。不过,这是用mongoshell语法编写的。它的PHP代码不起作用吗?你能告诉我们PHP代码吗?
5 年多之前 回复
dsv17139
dsv17139 这是一个语法错误,因此文档的格式在语法错误方面无关紧要
5 年多之前 回复
dongshou2024
dongshou2024 这应该工作正常。你能告诉我们你想要匹配的样本文件,但是不匹配吗?
5 年多之前 回复

1个回答

The query you posted should work. Just to verify, I inserted that data as a row in a test collection, and it was returned right back out with db.test.find({client: ''})

I'm thinking you may have a problem somewhere else. I've once or twice been in the wrong database when running queries, or had a typo in the collection name. To verify what my data looks like, I'll often times:

> 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.

Sometimes tunnel vision makes you think the problem lies somewhere that it doesn't. Leave a comment if this is or isn't helpful, maybe we can come up with some more troubleshooting ideas.

douhuo1738
douhuo1738 也许做一个var x = db.collection.findOne({_ id:ObjectId(“54eb59699e12a795078b80da”)}); 然后键入x.client,看看它返回什么?
5 年多之前 回复
duanqiang7631
duanqiang7631 它与错误的集合或任何拼写错误无关,但我认为我的文档可能是错误的。 我不认为这是一个空字符串。
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问