{"id":390,"date":"2022-03-29T17:59:12","date_gmt":"2022-03-29T09:59:12","guid":{"rendered":"http:\/\/gjweb.top\/?p=390"},"modified":"2022-03-29T18:00:56","modified_gmt":"2022-03-29T10:00:56","slug":"mongo%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=390","title":{"rendered":"Mongo\u8bed\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"\u6570\u636e\u5e93\u6982\u5ff5\u5bf9\u6bd4\">\u6570\u636e\u5e93\u6982\u5ff5\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>SQL<\/th><th>Mongo<\/th><\/tr><\/thead><tbody><tr><td>\u8868\uff08Table\uff09<\/td><td>\u96c6\u5408 \uff08collection\uff09<\/td><\/tr><tr><td>\u884c \uff08Row\uff09<\/td><td>\u6587\u6863 \uff08Document\uff09<\/td><\/tr><tr><td>\u5217 \uff08Col\uff09<\/td><td>\u5b57\u6bb5 \uff08Field\uff09<\/td><\/tr><tr><td>\u4e3b\u952e \uff08primary Key\uff09<\/td><td>\u5bf9\u8c61ID \uff08ObjectId\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u6570\u636e\u5e93\u64cd\u4f5c\">\u6570\u636e\u5e93\u64cd\u4f5c<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u521b\u5efa\u6570\u636e\u5e93<\/th><th>use demo<\/th><\/tr><\/thead><tbody><tr><td>\u67e5\u770b\u6570\u636e\u5e93<\/td><td>show dbs<\/td><\/tr><tr><td>\u5220\u9664\u6570\u636e\u5e93<\/td><td>db.dropDatabase()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u96c6\u5408\u64cd\u4f5c\">\u96c6\u5408\u64cd\u4f5c<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u521b\u5efa\u96c6\u5408<\/th><th>db.createCollection(name)<\/th><\/tr><\/thead><tbody><tr><td>\u67e5\u770b\u96c6\u5408<\/td><td>show collections<\/td><\/tr><tr><td>\u5220\u9664\u96c6\u5408<\/td><td>db.collection.drop()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u6587\u6863\u64cd\u4f5c\">\u6587\u6863\u64cd\u4f5c<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u521b\u5efa\u6587\u6863<\/th><th>db.collection.insertOne({}) db.collection.insertMany({})<\/th><\/tr><\/thead><tbody><tr><td>\u67e5\u770b\u6587\u6863<\/td><td>db.collections.find({}) db.collections.findOne({})<\/td><\/tr><tr><td>\u5220\u9664\u6587\u6863<\/td><td>db.collection.deleteOne() db.collection.deleteMany()<\/td><\/tr><tr><td>\u66f4\u65b0\u6587\u6863<\/td><td>db.collection.update({],{},false,true})<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u6761\u4ef6\u64cd\u4f5c\">\u6761\u4ef6\u64cd\u4f5c<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u5927\u4e8e<\/th><th>$gt<\/th><\/tr><\/thead><tbody><tr><td>\u5c0f\u4e8e<\/td><td>$lt<\/td><\/tr><tr><td>\u5927\u4e8e\u7b49\u4e8e<\/td><td>$gte<\/td><\/tr><tr><td>\u5c0f\u4e8e\u7b49\u4e8e<\/td><td>$lte<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u8fd4\u56de\u6570\u636e\u5e93\u6307\u5b9a\u7684\u5b57\u6bb5\">\u8fd4\u56de\u6570\u636e\u5e93\u6307\u5b9a\u7684\u5b57\u6bb5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u65b9\u5f0f\u4e00<br>const response = &nbsp;\u96c6\u5408\u540d\u79f0.findOne({<br> &nbsp; &nbsp;\u67e5\u8be2\u7684\u5b57\u6bb5:\u67e5\u8be2\u7684\u53c2\u6570,<br> &nbsp; &nbsp;...<br>},'\u8fd4\u56de\u7684\u5b57\u6bb51 \u5b57\u6bb52 \u5b57\u6bb53 ...')<br> &nbsp; &nbsp;<br>\/\/ \u65b9\u5f0f\u4e8c<br>\/**<br>* \u901a\u8fc7json\u65b9\u5f0f 1\u662f\u8fd4\u56de 0\u662f\u4e0d\u8fd4\u56de<br>*\/<br>const response = &nbsp;\u96c6\u5408\u540d\u79f0.findOne({<br> &nbsp; &nbsp;\u67e5\u8be2\u7684\u5b57\u6bb5:\u67e5\u8be2\u7684\u53c2\u6570,<br> &nbsp; &nbsp;...<br>},{key1:1,key2:1,...})<br>\u200b<br> \/\/ \u65b9\u5f0f\u4e09<br> \/*<br> * \u901a\u8fc7 select(\"key\")<br> *\/<br> &nbsp;const response = \u96c6\u5408\u540d\u79f0.findOne({<br> &nbsp; &nbsp; \u67e5\u8be2\u7684\u5b57\u6bb5\uff1a\u67e5\u8be2\u7684\u53c2\u6570,<br> &nbsp; &nbsp; ...<br>  }).select('key','key')<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u6279\u91cf\u66f4\u65b0\u6570\u636e\u6280\u5de7\">\u6279\u91cf\u66f4\u65b0\u6570\u636e\u6280\u5de7<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u65b9\u5f0f\u4e00<br>\/*<br>* \u8fd9\u91cc\u7684User\u662f\u5b9a\u4e49\u597d\u7684\u6570\u636e\u6a21\u578b<br>*\/<br>const User = require('.\/..\/models\/userSchema')<br>const res = &nbsp;User.updateMany({ $or: &#91;{ userId: 10001 }, { userId: 10002 }] },{state:2})<br>\/*<br>* \u8868\u793a\u5c06userId 10001,10002\u6570\u636e\u4e2d\u7684 state \u6539\u4e3a 2<br>*\/<br>\u200b<br>\/\/ \u65b9\u5f0f\u4e8c<br>const User = require('.\/..\/models\/userSchema')<br>const res = User.updateMany({userId:{$in:&#91;10001,10002]}},{state:2})<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4e24\u79cd\u65b9\u6cd5\u90fd\u4f1a\u8fd4\u56de\u5220\u9664\u540e\u7684\u6761\u6570<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u67e5\u627e\u5e76\u66f4\u65b0\u6570\u636e\">\u67e5\u627e\u5e76\u66f4\u65b0\u6570\u636e<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>const User = require('.\/..\/models\/userSchema')<br>\/*<br>* \u8fd9\u91cc\u7684User\u662f\u5b9a\u4e49\u597d\u7684\u6570\u636e\u6a21\u578b<br>* \u67e5\u627e User\u96c6\u5408\u4e2d userId\u4e3a100\u7684\u6570\u636e\uff0c\u66f4\u65b0\u5b57\u6bb5 mobile\u3001job \u4e24\u4e2a\u5b57\u6bb5<br>*\/<br>const res = await User.findOneAndUpdate({ userId:\"100\" }, { mobile:123, job:456 });<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"findoneandupdate-\u4f7f\u7528\u573a\u666f-\u81ea\u589eid\">findOneAndUpdate \u4f7f\u7528\u573a\u666f&#8212;-\u81ea\u589eID<\/h3>\n\n\n\n<p>\u65b0\u5efa\u6570\u636e\u6a21\u578b counterSchema<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u200b<br>\/**<br> *\u7ef4\u62a4\u7528\u6237ID\u81ea\u589e\u957f\u8868<br> *\/<br>const mongoose = require(\"mongoose\")<br>const userSchema = &nbsp;mongoose.Schema({<br> &nbsp;_id:String,<br> &nbsp;sequence_value:Number<br>})<br>module.exports = mongoose.model(\"counter\",userSchema,\"counters\")<br>\/**\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570<br> * \u6a21\u578b\u540d\u79f0<br> * \u6a21\u578b<br> * \u96c6\u5408\u540d\u79f0<br> *\/<br>\u200b<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const doc = await Counter.findOneAndUpdate({ _id: 'userId' }, { $inc: { sequence_value: 1 } }, { new: true })<br>\/*<br>*  \u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u8981\u81ea\u589e\u7684\u5b57\u6bb5\u3002{ _id: 'userId' }<br>*  \u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a $inc\u4fee\u9970\u7b26 sequence_value \u5b57\u6bb5\u52a0\u4e00 <br>*  \u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a new:true \u8868\u793a\u8fd4\u56de\u66f4\u65b0\u540e\u7684\u5bf9\u8c61\uff0c\u4e3afalse\u8868\u793a\u66f4\u65b0\u524d\u7684\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u662ffalse<br>*\/<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u67e5\u627e\u76f8\u5173\u7684\u6280\u5de7\">\u67e5\u627e\u76f8\u5173\u7684\u6280\u5de7<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*<br>* &nbsp; \u8fd9\u91cc\u7684User\u662f\u5b9a\u4e49\u597d\u7684\u6570\u636e\u6a21\u578b<br>* &nbsp; \u4f7f\u7528 $or \u4fee\u9970\u7b26 {$or:&#91;{\u67e5\u627e\u7684key:\u67e5\u627e\u7684value,key:value}]},\"\u8868\u793a\u8fd4\u56de\u7684\u5b57\u6bb51 \u8fd4\u56de\u7684\u5b57\u6bb52 \u8fd4\u56de\u7684\u5b57\u6bb53\"<br>*\/<br>const User = require('.\/..\/models\/userSchema')<br>const res = await User.findOne({$or:&#91;{userName:\"admin\",userEmail:\"admin@qq.com\"}]},\"_id userName userEmail\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u521b\u5efa\u6216\u63d2\u5165\u6587\u6863\">\u521b\u5efa\u6216\u63d2\u5165\u6587\u6863<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*<br>* \u8fd9\u91cc\u7684User\u662f\u5b9a\u4e49\u597d\u7684\u6570\u636e\u6a21\u578b<br>*\/<br>const User = require('.\/..\/models\/userSchema')<br>const user = new User({<br> &nbsp; &nbsp; &nbsp; &nbsp;userId:doc.sequence_value,<br> &nbsp; &nbsp; &nbsp; &nbsp;userName,<br> &nbsp; &nbsp; &nbsp; &nbsp;userPwd:md5('admin'),<br> &nbsp; &nbsp; &nbsp; &nbsp;userEmail,<br> &nbsp; &nbsp; &nbsp; &nbsp;role:1,\/\/ \u9ed8\u8ba4\u4e3a\u666e\u901a\u7528\u6237<br> &nbsp; &nbsp; &nbsp; &nbsp;roleList,<br> &nbsp; &nbsp; &nbsp; &nbsp;job,<br> &nbsp; &nbsp; &nbsp; &nbsp;state,<br> &nbsp; &nbsp; &nbsp; &nbsp;deptId,<br> &nbsp; &nbsp; &nbsp; &nbsp;mobile<br>})<br>user.save()<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u5e93\u6982\u5ff5\u5bf9\u6bd4 SQL Mongo \u8868\uff08Table\uff09 \u96c6\u5408 \uff08collection\uff09 \u884c \uff08Row\uff09 \u6587\u6863 \uff08 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-390","post","type-post","status-publish","format-standard","hentry","category-monggodb"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=390"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/390\/revisions"}],"predecessor-version":[{"id":391,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/390\/revisions\/391"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}