developer zone - ½Ì³Ì - mongodbÖ¸ÄÏ£¨·Ò룩£¨ËÄ£©
ÔËÐÐmongodb
Çë²ÎÓë¿ìËÙÆô¶¯Ö¸ÄÏÀ´Æô¶¯mongodb¡£
Á¬½Óµ½Êý¾Ý¿â
Ê×ÏÈʹÓÃÊý¾Ý¿âshell³ÌÐòÀ´²Ù×÷Êý¾Ý¿â¡££¨ÎÒÃÇͬÑù¿ÉÒÔʹÓÃÈκÎÓïÑÔµÄÇý¶¯À´×öÀàËƵIJÙ×÷¡£shell³ÌÐò¿ÉÒԺܷ½±ãµÄ½øÐн»»¥ºÍ¹ÜÀí¡££©
ÕâÑùÆô¶¯mongodb JavaScript shell:
# 'mongo' is shell binary. exact location might vary depending on # installation method and platform $ bin/mongoĬÈÏÇé¿öÏÂshell³ÌÐò»áÁ¬½Óµ½±¾µØµÄÊý¾Ý¿â¡°test¡±¡£Ä㽫¿´µ½£º
MongoDB shell version: <whatever> url: test connecting to: test type "help" for help >"connecting to:"¸æËßÄãÄ¿Ç°ÕýÔÚʹÓõÄÊý¾Ý¿âÃû³Æ¡£¿ÉÒÔÕâÑùÇл»Êý¾Ý¿â£º
> use mydb switched to db mydbÇл»Êý¾Ý¿âµÄuseÃüÁî²¢²»»áÁ¢¼´´´½¨Õâ¸öÊý¾Ý¿â-Êý¾Ý¿âÑÓ³Ùµ½Êý¾ÝµÚÒ»´Î²åÈëµÄʱºò´´½¨¡£ÕâÒâζ×ÅÈç¹ûÄãuseij¸öÊý¾Ý¿â£¬µ«ÊÇshow dbsÃüÁî²¢²»»á½«ËüÁгöÀ´³ý·ÇÓÐÊý¾Ý²åÈë¡£
ÒªÏë²é¿´¿ÉÒÔʹÓõÄÃüÁîÁÐ±í£¬¿ÉÒÔÇÃÈëhelp.
¶¯Ì¬Ä£Ê½£¨Ä£Ê½×ÔÓÉ£©
MongoDBÏñ´«Í³µÄRDBMSÒ»ÑùÓµÓÐÊý¾Ý¿â¡¢¼¯ºÏºÍË÷Òý¡£ÔÚһЩ³¡ºÏ£¨Êý¾Ý¿âºÍ¼¯ºÏ£©ÕâЩ¶ÔÏó¿ÉÒÔ±»Òþʽ´´½¨£¬²»¹ýÒ»µ©´´½¨ËüÃǾʹæÔÚÓÚÒ»¸öϵͳĿ¼ÖУ¨db.systems.collections, db.system.indexes£©¡£
¼¯ºÏÖаüº¬ÁË£¨BSON£©Îĵµ¡£ÔÚÕâЩÎĵµÖÐÊÇһЩÓò¡£ÔÚmongodbÖÐûÓÐÔ¤¶¨ÒåµÄÓò£¨¶ÔÓ¦ÓÚRDBMSÖеÄÁУ©¡£¶ÔÓÚÎĵµÄÚµÄÓòûÓÐÈκÎģʽ ÒªÇó-ÕâЩÓòºÍËûÃǵÄÖµÀàÐÍ¿ÉÒÔÊÇÈκÎÀàÐÍ¡£ÔÚÕâÀïÔö¼ÓÒ»ÁÐҲûÓС°alter table¡±µÄ¸ÅÄî¡£¾¡¹ÜûÓÐÕâÑùÒ»¸öÒªÇ󣬵«Êµ¼ÊÓ¦ÓÃÖУ¬ÔÚÒ»¸ö¼¯ºÏÖеÄÎĵµÓµÓÐÏàÀàËƵĽṹÊǷdz£ÆÕ±éµÄ¡£ÕâÖÖÁé»îÐÔÒâζ×ÅģʽǨÒƺÍÔö´óÔÚʵ¼ÊÖÐ·Ç ³£ÈÝÒ×-ºÜÉÙ»áÐèÒªÄãȥд½Å±¾×öÀàËÆÓÚ¡°alter table¡±µÄ²Ù×÷¡£³ýÁËÈÃģʽǨÒÆÁé»îÍ⣬ÕâÖÖ±ãÀûʹµÃÔÚÊý¾Ý¿âÖ®ÉÏ×öµü´ú¿ª·¢¸ü¼ÓÈÝÒס£
Ïò¼¯ºÏÖвåÈëÊý¾Ý
ÎÒÃÇÊ×ÏÈ´´½¨Ò»¸ö¼¯ºÏÈ»ºóÏòÀïÃæ²åÈëһЩÊý¾Ý¡£ÎÒÃǻᴴ½¨Á½¸ö¶ÔÏó£¬jºÍt£¬È»ºó½«ËûÃDZ£´æÔÚ¼¯ºÏthinsÖС£
ÔÚ½ÓÏÂÀ´µÄÀý×ÓÖУ¬">"´ú±íshellÖÐÊäÈëÃüÁîµÄÌáʾ·û¡£
> j = { name : "mongo" }; {"name" : "mongo"} > t = { x : 3 }; { "x" : 3 } > db.things.save(j); > db.things.save(t); > db.things.find(); { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" } { "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 } >Óм¸µãÐèҪעÃ÷£º
- ÎÒÃÇûÓÐÔ¤ÏÈ´´½¨Õâ¸ö¼¯ºÏ¡£ÔÚÎÒÃÇÊ״βåÈëÊý¾ÝʱÊý¾Ý¿â×Ô¶¯´´½¨ÁËËü¡£
- ±£´æµÄÎĵµ¿ÉÒÔÓµÓв»Í¬µÄ½á¹¹-ʵ¼ÊÉÏÔÚÕâ¸öÀý×ÓÖУ¬ÕâÁ½¸öÎĵµ¸ù±¾Ã»ÓÐÈκÎÏàͬµÄÊý¾ÝÔªËØ¡£ÔÚʵ¼ÊÓ¦ÓÃÖУ¬ÔÚÒ»¸ö¼¯ºÏÖб£´æµÄÊý¾Ýͨ³£¾ßÓÐÏàͬµÄ½á¹¹¡£
- ÔÚ²åÈëµ½Êý¾Ý¿â֮ǰ£¬¶ÔÏóµÄÓò_id±»¸³ÓèÁËÒ»¸öobject ID(Èç¹û»¹Ã»ÓÐÕâ¸öÓò).
- µ±ÄãÔËÐÐÉÏÃæµÄÀý×Óʱ£¬ÄãµÄObjectIDÖµ¿ÉÄܻ᲻ͬ¡£
ÈÃÎÒÃÇÔö¼Ó¸ü¶àµÄ¼Ç¼µ½¸Ã¼¯ºÏÖУº
> for (var i = 1; i <= 20; i++) db.things.save({x : 4, j : i}); > db.things.find(); { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" } { "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 } { "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 } { "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 } { "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 } { "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 } { "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 } { "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 } { "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 } { "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 } { "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 } { "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 } { "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 } { "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 } { "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 } { "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 } { "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 } { "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 } { "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 } { "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 } has more×¢Òâ²¢²»ÊÇËùÓеÄÎĵµ¶¼»áÏÔʾ³öÀ´-µ±×Ô¶¯µü´úÒ»¸öÓαêʱ£¬shell½«·µ»ØÊýÄ¿ÏÞÖÆÔÚ20.ÓÉÓÚÎÒÃÇÒѾÔÚ¼¯ºÏÖвåÈëÁË2¸öÎĵµ£¬Òò´ËÎÒÃÇÖ»¿´µ½ÁË18¸öвåÈëµÄÎĵµ¡£
Èç¹ûÎÒÃÇÏëÒª·µ»ØÏÂÒ»×é½á¹û£¬¿ÉÒÔʹÓÿì½Ý·½Ê½it¡£½Ó×ÅÉÏÃæµÄ´úÂ룺
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 } { "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 } has more > it { "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 } { "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }¼¼ÊõÉÏ£¬find()·µ»ØÒ»¸öÓαê¶ÔÏó¡£µ«ÔÚÉÏÃæµÄʵÀýÖУ¬ÎÒÃDz¢Ã»Óн«Õâ¸öÓα긳ֵµ½Ò»¸ö±äÁ¿¡£Òò´Ë£¬shell×Ô¶¯µü´úÁËÕâ¸öÓα꣬·µ»ØÒ»×é³õʼµÄ½á¹û£¬²¢ÔÊÐíÎÒÃÇʹÓÃitÃüÁî¼ÌÐøµü´ú¡£
µ«ÊÇÎÒÃÇ»¹¿ÉÒÔÖ±½ÓʹÓÃcursor£»ÔÚÏÂÃæµÄÕ½ÚÖлáÌÖÂÛ¸ÃÔõô×ö¡£
´Ó²éѯÖлñÈ¡Êý¾Ý
Ç°ÃæÎÒÃÇÌÖÂÛÁËÈκÎÉî¶ÈµÄ²éѯ£¬½ÓÏÂÀ´ÎÒÃǽ²½²Èç¹ûʹÓòéѯ½á¹û¹¤×÷-Ò»¸öÓαê¶ÔÏó¡£ÎÒÃǽ«Ê¹Óüòµ¥µÄfind()²éѯº¯Êý£¬Ëü»á·µ»Ø¼¯ºÏÖеÄÈκÎÄÚÈÝ£¬ºóÃæ»á½²Èç¹û´´½¨ÌØÊâµÄ²éѯ¡£
ΪÁËʹÓÃmongo shellÀ´²é¿´¼¯ºÏÖеÄËùÓÐÔªËØ£¬ÎÒÃÇÐèÒªÏÔʽµÄʹÓÃfind()²Ù×÷·µ»ØµÄÓαꡣ
ÈÃÎÒÃÇÔÙ×öÒ»´ÎÏàͬµÄ²éѯ£¬µ«ÊÇÕâ´ÎʹÓÃfind()·µ»ØµÄÓα꣬ȻºóÔÚwhileÑ»·Öеü´úËü£º
> var cursor = db.things.find(); > while (cursor.hasNext()) printjson(cursor.next()); { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" } { "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 } { "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 } { "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 } { "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 } { "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 } { "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 } { "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 } { "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 } { "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 } { "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 } { "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 } { "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 } { "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 } { "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 } { "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 } { "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 } { "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 } { "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 } { "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 } { "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 } { "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }ÉÏÃæµÄÀý×ÓÏÔʾÁËÓαê·ç¸ñµÄµü´ú¡£hasNext()º¯Êý¸æËßÎÒÃÇÊÇ·ñ»¹ÓÐÎĵµ¿ÉÒÔ·µ»Ø£¬next()º¯ÊýÔò·µ»ØÏÂÒ»¸öÎĵµ¡£ÎÒÃÇ»¹Ê¹ÓÃÁËÄÚÖõÄprintjson()·½·¨½«Îĵµ·ÒëΪƯÁÁµÄJSON·ç¸ñµÄ¸ñʽ¡£
µ±ÔÚJavaScript shellÏÂÃ湤×÷£¬ÎÒÃÇ»¹¿ÉÒÔʹÓøÃÓïÑÔʵÓõĻù±¾×é¼þ£¬È»ºó¶ÔÓαêµ÷ÓÃforEach¡£Öظ´ÉÏÃæµÄÀý×Ó£¬µ«ÊǶÔÓα겻ÔÙʹÓÃwhileÑ»·£¬¶øÊÇÖ±½ÓʹÓÃforEach()£º
> db.things.find().forEach(printjson); { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" } { "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 } { "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 } { "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 } { "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 } { "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 } { "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 } { "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 } { "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 } { "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 } { "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 } { "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 } { "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 } { "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 } { "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 } { "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 } { "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 } { "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 } { "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 } { "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 } { "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 } { "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }ÔÚforEach()µÄʵÀýÖУ¬ÎÒÃDZØÐëΪÓα궨ÒåÒ»¸öº¯Êý£¬¶ÔÓÚÿһ¸öÎĵµËü¶¼»á±»µ÷Óá£
ÔÚmongo shell,Äã´¦ÀíÓα껹¿ÉÒÔÏñ´¦ÀíÊý×飺
> var cursor = db.things.find(); > printjson(cursor[4]); { "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }µ±ÒÔÕâÖÖ·½Ê½Ê¹ÓÃÓαêʱ£¬×¢ÒâËùÓеÄÖµÖ±µ½·ÃÎʵÄ×î´óÖµ£¨ÉÏÃæÊÇcursor[4]£©¶¼»á±»Í¬Ê±×°Ôص½RAMÖС£Õâ¶ÔÓںܴóµÄ½á¹û¼¯ÊDz»ºÏÊʵģ¬ÒòΪÄã»áºÄ¾¡ÄÚ´æ¡£¶ÔÓÚÈκηµ»Ø´óÁ¿ÔªËصIJéѯ£¬ÓαêÓ¦µ±×÷Ϊµü´úÆ÷À´Ê¹Óá£
³ýÁËÒÔÊý×é·ç¸ñÀ´·ÃÎÊÓα꣬Äã¿ÉÒÔ½«Óαêת»»ÎªÒ»¸öÕæÕýµÄÊý×飺
> var arr = db.things.find().toArray(); > arr[5]; { "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }Çë×¢ÒâÕâÖÖÊý×éÌØÐÔ½öÊÊÓÃÓÚmongo-½»»¥ÐÍshell£¬²¢²»ÊÇËùÓеÄÇý¶¯¶¼ÌṩÕâÖÖ¹¦ÄÜ¡£
mongodbÓα겻ÊÇ¿ìÕÕ-ÔÚÄãµÚÒ»´ÎºÍ×îºóÒ»´Îµ÷ÓÃnext()Öм䣬Óαê¿ÉÄÜ·µ»Ø»òÕß²»·µ»ØÄã»òÕßÆäËûÓû§ÔÚÕâ¸ö¼¯ºÏÖÐËùÖ´ÐеIJÙ×÷¡£Ê¹ÓÃÏÔʽµÄËø¶¯×÷À´Ö´ÐÐÒ»¸ö¿ìÕÕÐ͵IJéѯ¡£
Ö¸¶¨²éѯµÄ·µ»ØÖµ
ÏÖÔÚÎÒÃÇÒѾ֪µÀÁËÈç¹ûʹÓòéѯ·µ»ØµÄÓα깤×÷£¬ÈÃÎÒÃǹØ×¢Ôõô¶¨ÖƲéѯȥ·µ»ØÌض¨µÄÄÚÈÝ¡£
ͨ³£µÄ·½·¨¾ÍÊÇ´´½¨"²éѯÎĵµ"£¬Õâ¸ö²éѯÎĵµÖ¸³öÁËÐèҪƥÅäµÄ¼üºÍÖµµÄÑùʽ¡£
Ïà±ÈÓÚ½âÊÍ£¬ÕâЩ¸üÈÝÒ×ÑÝʾ¡£ÔÚ½ÓÏÂÀ´µÄÀý×ÓÖУ¬ÎÒÃÇ»á¸ø³öSQL²éѯÀý×Ó£¬È»ºóÑÝʾÈçºÎͨ¹ýmongo shellÀ´±í´ïÏàͬµÄ²éѯ¡£ÕâÖÖÖ¸¶¨²éѯµÄ·½·¨ÔÚmongodbÖÐÊǺܻù´¡µÄ£¬ËùÒÔÄ㽫ÄÜÔÚËùÓеÄÇý¶¯»òÕßÓïÑÔÖÐÕÒµ½ÏàͬµÄͨÓõŦÄÜ¡£
SELECT * FROM things WHERE name="mongo" > db.things.find({name:"mongo"}).forEach(printjson); { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" } SELECT * FROM things WHERE x=4 > db.things.find({x:4}).forEach(printjson); { "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 } { "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 } { "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 } { "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 } { "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 } { "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 } { "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 } { "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 } { "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 } { "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 } { "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 } { "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 } { "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 } { "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 } { "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 } { "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 } { "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 } { "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 } { "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 } { "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
²éѯ±í´ïʽ¾ÍÊÇÎĵµ±¾Éí¡£ÐÎÈç{a:A,b:B,...}µÄ²éѯÎĵµÒâ˼ÊÇ"where a--A and b==B and...".¸ü¶à¹ØÓÚ²éѯÄÜÁ¦µÄÐÅÏ¢¿ÉÒÔ²ÎÔIJéѯºÍÓαꡣ
MongoDB»¹ÔÊÐíÖ»·µ»Ø"²¿·ÖÎĵµ"-ÕâЩÎĵµ½ö°üº¬Êý¾Ý¿âÖÐÎĵµÔªËصÄÒ»¸ö×Ó¼¯¡£
ʵÏÖÕâ¸ö¹¦ÄÜ£¬ÄãÔÚfind()²éѯº¯ÊýÖÐÔö¼ÓµÚ¶þ¸ö²ÎÊý£¬Ìṩһ¸öÐèÒª·µ»ØÔªËØÁбíµÄÎĵµ¾Í¿ÉÒÔÁË¡£
ΪÁËÑÝʾÕâ¸ö¹¦ÄÜ£¬ÎÒÃÇʹÓÃÔö¼ÓÒ»¸öÏÞÖƽö·µ»Ø¡±j¡°ÔªËصIJÎÊýÀ´Öظ´ÉÏÒ»¸öfind({x:4})µÄÀý×Ó:
SELECT j FROM things WHERE x=4 > db.things.find({x:4}, {j:true}).forEach(printjson); { "_id" : ObjectId("4c220a42f3924d31102bd856"), "j" : 1 } { "_id" : ObjectId("4c220a42f3924d31102bd857"), "j" : 2 } { "_id" : ObjectId("4c220a42f3924d31102bd858"), "j" : 3 } { "_id" : ObjectId("4c220a42f3924d31102bd859"), "j" : 4 } { "_id" : ObjectId("4c220a42f3924d31102bd85a"), "j" : 5 } { "_id" : ObjectId("4c220a42f3924d31102bd85b"), "j" : 6 } { "_id" : ObjectId("4c220a42f3924d31102bd85c"), "j" : 7 } { "_id" : ObjectId("4c220a42f3924d31102bd85d"), "j" : 8 } { "_id" : ObjectId("4c220a42f3924d31102bd85e"), "j" : 9 } { "_id" : ObjectId("4c220a42f3924d31102bd85f"), "j" : 10 } { "_id" : ObjectId("4c220a42f3924d31102bd860"), "j" : 11 } { "_id" : ObjectId("4c220a42f3924d31102bd861"), "j" : 12 } { "_id" : ObjectId("4c220a42f3924d31102bd862"), "j" : 13 } { "_id" : ObjectId("4c220a42f3924d31102bd863"), "j" : 14 } { "_id" : ObjectId("4c220a42f3924d31102bd864"), "j" : 15 } { "_id" : ObjectId("4c220a42f3924d31102bd865"), "j" : 16 } { "_id" : ObjectId("4c220a42f3924d31102bd866"), "j" : 17 } { "_id" : ObjectId("4c220a42f3924d31102bd867"), "j" : 18 } { "_id" : ObjectId("4c220a42f3924d31102bd868"), "j" : 19 } { "_id" : ObjectId("4c220a42f3924d31102bd869"), "j" : 20 }×¢£ºÄ¬ÈÏÇé¿öÏÂ"_id"ÓòʼÖջ᷵»Ø¡£
findOne()-Óï·¨±ãÀû
¼ò±ãÆð¼û£¬mongo shell(ºÍÆäËûµÄÇý¶¯)ÔÊÐíͨ¹ýfindOne()º¯Êý½ö·µ»ØÒ»¸öÎĵµ£¬±ÜÃâÁ˳ÌÐò´ÓÍ·¿ªÊ¼´¦ÀíÓαꡣfindOne()½ÓÊÜÓëfind()·½·¨Ïàͬ µÄ²ÎÊý£¬µ«ÊDz»ÔÙ·µ»ØÓα꣬¶øÊÇ·µ»ØҪôÊý¾Ý¿âÖеÚÒ»¸ö·ûºÏµÄÎĵµ£¬ÒªÃ´·µ»ØnullÈç¹ûûÓÐÆ¥Åäµ½¡£
×÷ΪÀý×Ó£¬ÎÒÃǼìË÷Ò»¸ö·ûºÏ"name=="mongo""µÄÎĵµ¡£³ýÁ˶ÔÓαêµ÷ÓÃnext()£¨µ±È»Ê×ÏÈÅжÏÊÇ·ñΪ¿Õ£©»òÕß½«Óα굱×öÊý×é´¦Àí¶ÁÈ¡0thÔªËØ£¬»¹Óкܶ෽·¨¿ÉÒÔʹÓá¢
ÎÞÂÛÈçºÎ£¬findOne()·½·¨·½±ã²¢ÇÒ¸ßЧ£º
> printjson(db.things.findOne({name:"mongo"})); { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }Õâ¸ö·½·¨¸ü¼Ó¸ßЧ£¬ÊÇÒòΪ¿Í»§¶ËÏòÊý¾Ý¿âÇëÇ󵥸ö¶ÔÏó£¬ÕâÑùÊý¾Ý¿âºÍÍøÂçºÍÒÔÇ°Ïà±ÈÖ»×öÉÙÁ¿µÄ¹¤×÷¡£ËüµÈЧÓÚfind({name:"mogno"}).limit(1).
ÁíÍâÒ»¸öʹÓÃ_id²éѯµ¥¸öÎĵµµÄÀý×Ó£º
> var doc = db.things.findOne({_id:ObjectId("4c2209f9f3924d31102bd84a")}); > doc { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
ʹÓÃlimit()ÏÞÖÆ·µ»Ø¸öÊý
Äã¿ÉÒÔͨ¹ýlimit()º¯ÊýÀ´É趨·µ»Ø½á¹ûµÄ×î´ó¸öÊý£¬´Ó¶øÏÞ¶¨Á˲éѯ½á¹ûµÄÊý¾Ý´óС¡£
ÓÉÓÚÐÔÄܵÄÔÒòËüÊÇÇ¿ÁÒÍƼöʹÓõģ¬ÕâÑù¾ÍÏÞ¶¨ÁËÊý¾Ý¿âÐèÒª×öµÄ¹¤×÷£¬ÏÞ¶¨ÁËÐèҪͨ¹ýÍøÂç·µ»ØµÄÊý¾ÝÁ¿´óС¡£ÀýÈ磺
> db.things.find().limit(3); { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" } { "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 } { "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
¸ü¶à°ïÖú
³ýÁËʹÓÃ"help"ÃüÁÄ㻹¿ÉÒÔÔÚdbºÍdb.whateverÉÏÃæµ÷ÓÃhelpÈ¥²é¿´¿ÉÓõķ½·¨¡£
Èç¹ûÄãºÜºÃÆæij¸öº¯ÊýÄÚ²¿ÔÚ×öʲô£¬Äã¿ÉÒÔÊäÈ뺯ÊýÃû£¨²»Òª()£©shell»á´òÓ¡³öÔ´´úÂ룬ÀýÈ磺
> printjson function (x) { print(tojson(x)); }mongoÊÇÒ»¸öÍêÈ«µÄJavaScript shell£¬Òò´ËÈκÎJavaScriptº¯Êý£¬Óï·¨£¬»òÕßÀ඼¿ÉÒÔÔÚ¸ÃshellÖÐʹÓá£ÁíÍ⣬mongodbÒ²¶¨ÒåÁËһЩ×Ô¼ºµÄÀàºÍÈ«¾Ö±äÁ¿£¨Èçdb£©. Äã¿ÉÒÔhttp://api.mongodb.org/js/Ôڲ鿴ÍêÕûµÄAPI.
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ