dongtan4046 2015-02-25 19:06
浏览 205
已采纳

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

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.

  • 写回答

1条回答

  • dongtui0650 2015-02-28 12:36
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)