MongoDB小技巧之如何使用帶下劃線的集合名查詢

chenfeng發表於2017-09-29
> show collections;
_test
_test_serial_number
_test_message_send
inventory
>


> db._test_message_send.find()
2017-09-29T01:52:37.612+0000 E QUERY    [thread1] TypeError: db._wechat_message_send is undefined :
@(shell):1:1


> db._test_message_send.count()
2017-09-29T01:53:25.302+0000 E QUERY    [thread1] TypeError: db._wechat_message_send is undefined :
@(shell):1:1

帶下劃線的集合名字查詢的時候應使用getCollection,例如:

> db.getCollection('_test_message_send').count()
2562

> db.getCollection('_test_message_send').findOne()
{
        "_id" : "93e4cc65502344f584bfb385f94dde35",
        "_class" : "com.base.entities.WechatMessageSend",
        "type" : 4,
        "status" : 3,
        "memberId" : "863eee3694fc45",
        "openId" : "oB3wuwViNwa2G2NpOCdjMSvFJCcY",
        "templateId" : "2KKEiTYulLq4RxRudlNuX",
        "messageId" : "7a33b0f20e2e43c",
        "createTime" : ISODate("2016-07-19T00:23:37.346Z")
}

重新命名集合名字:
> db.getCollection('_test').renameCollection("_test_bak20171011");
{ "ok" : 1 }



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2145584/,如需轉載,請註明出處,否則將追究法律責任。

相關文章