{"id":336,"date":"2026-02-11T11:37:17","date_gmt":"2026-02-11T03:37:17","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=336"},"modified":"2026-06-12T11:40:56","modified_gmt":"2026-06-12T03:40:56","slug":"mongose-%e7%ae%80%e5%8d%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/02\/11\/mongose-%e7%ae%80%e5%8d%95%e4%bd%bf%e7%94%a8\/","title":{"rendered":"mongose \u7b80\u5355\u4f7f\u7528"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1.\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install mongoose --save<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a><a><\/a>2.\u8fde\u63a5mongodb<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const mongoose = require(\"mongoose\")\nmongoose.connect(\"mongodb:\/\/&#91;username]:&#91;password]@&#91;host]:&#91;port]\/&#91;dbname]\",{\n   useNewUrlParser: true\n})<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a><a><\/a>3.\u521b\u5efa\u6a21\u578b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const mongoose = require(\"mongoose\")<em>\/\/\u8fd9\u91cc\u6240\u5f15\u7528\u7684mongose\u662f\u4e0a\u9762\u8fde\u63a5\u521b\u5efa\u51fa\u6765\u7684mongose\u5e38\u91cf<\/em>\nconst schema = new mongoose.Schema({\n   type:{type:String},\n   name:{type:String},\n})\nmodule.exports = mongoose.model(\"BillSet\",schema)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bbe\u7f6e\u5b57\u6bb5\u4e0d\u67e5\u8be2,\u5e76\u4e14\u5199\u5165\u8f6c\u6362<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const mongoose = require(\"mongoose\")\nconst schema = new mongoose.Schema({\n   username:{type:String},\n   password:{type: String,select:false,set(val){\n        return require('bcrypt').hashSync(val,10)\n    }}\n})\n\nmodule.exports = mongoose.model(\"AdminUser\",schema)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a><a><\/a>4.\u6570\u636e\u64cd\u4f5c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u5148\u83b7\u53d6\u5b9a\u4e49\u7684mongo\u6a21\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const modelName = require(\"inflection\").classify(req.params.resource)\nreq.model = require(`..\/models\/${modelName}`)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a><\/a><a><\/a>4.1 \u67e5\u8be2<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\ud835\udc5c\ud835\udc5f\u6216\u5173\u7cfb<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>o<\/mi><mi>r<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u6216<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5173<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7cfb<\/mo><\/mrow><\/math>nor \u6216\u5173\u7cfb\u53d6\u53cd<br>\ud835\udc54\ud835\udc61\u5927\u4e8e<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>g<\/mi><mi>t<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5927<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e8e<\/mo><\/mrow><\/math>gte \u5927\u4e8e\u7b49\u4e8e<br>\ud835\udc59\ud835\udc61\u5c0f\u4e8e<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>l<\/mi><mi>t<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5c0f<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e8e<\/mo><\/mrow><\/math>lte \u5c0f\u4e8e\u7b49\u4e8e<br>\ud835\udc5b\ud835\udc52\u4e0d\u7b49\u4e8e<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>n<\/mi><mi>e<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e0d<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7b49<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e8e<\/mo><\/mrow><\/math>in \u5728\u591a\u4e2a\u503c\u8303\u56f4\u5185<br>\ud835\udc5b\ud835\udc56\ud835\udc5b\u4e0d\u5728\u591a\u4e2a\u503c\u8303\u56f4\u5185<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>n<\/mi><mi>i<\/mi><mi>n<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e0d<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5728<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u591a<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e2a<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u503c<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8303<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u56f4<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5185<\/mo><\/mrow><\/math>all \u5339\u914d\u6570\u7ec4\u4e2d\u591a\u4e2a\u503c<br>\ud835\udc5f\ud835\udc52\ud835\udc54\ud835\udc52\ud835\udc65\u6b63\u5219\uff0c\u7528\u4e8e\u6a21\u7cca\u67e5\u8be2<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>r<\/mi><mi>e<\/mi><mi>g<\/mi><mi>e<\/mi><mi>x<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u6b63<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5219<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff0c<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7528<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e8e<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u6a21<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7cca<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u67e5<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8be2<\/mo><\/mrow><\/math>size&nbsp;\u5339\u914d\u6570\u7ec4\u5927\u5c0f<br>\ud835\udc5a\ud835\udc4e\ud835\udc65\ud835\udc37\ud835\udc56\ud835\udc60\ud835\udc61\ud835\udc4e\ud835\udc5b\ud835\udc50\ud835\udc52\u8303\u56f4\u67e5\u8be2\uff0c\u8ddd\u79bb\uff08\u57fa\u4e8e\ud835\udc3f\ud835\udc35\ud835\udc46\uff09<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>m<\/mi><mi>a<\/mi><mi>x<\/mi><mi>D<\/mi><mi>i<\/mi><mi>s<\/mi><mi>t<\/mi><mi>a<\/mi><mi>n<\/mi><mi>c<\/mi><mi>e<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8303<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u56f4<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u67e5<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8be2<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff0c<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8ddd<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u79bb<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff08<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u57fa<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e8e<\/mo><\/mrow><mi>L<\/mi><mi>B<\/mi><mi>S<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff09<\/mo><\/mrow><\/math>mod\u3000\u3000\u3000\u3000 \u53d6\u6a21\u8fd0\u7b97<br>\ud835\udc5b\ud835\udc52\ud835\udc4e\ud835\udc5f\u90bb\u57df\u67e5\u8be2\uff0c\u67e5\u8be2\u9644\u8fd1\u7684\u4f4d\u7f6e\uff08\u57fa\u4e8e\ud835\udc3f\ud835\udc35\ud835\udc46\uff09<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>n<\/mi><mi>e<\/mi><mi>a<\/mi><mi>r<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u90bb<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u57df<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u67e5<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8be2<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff0c<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u67e5<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8be2<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u9644<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8fd1<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7684<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4f4d<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7f6e<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff08<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u57fa<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e8e<\/mo><\/mrow><mi>L<\/mi><mi>B<\/mi><mi>S<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff09<\/mo><\/mrow><\/math>exists \u5b57\u6bb5\u662f\u5426\u5b58\u5728<br>\ud835\udc52\ud835\udc59\ud835\udc52\ud835\udc5a\ud835\udc40\ud835\udc4e\ud835\udc61\ud835\udc50\u210e\u5339\u914d\u5185\u6570\u7ec4\u5185\u7684\u5143\u7d20<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>e<\/mi><mi>l<\/mi><mi>e<\/mi><mi>m<\/mi><mi>M<\/mi><mi>a<\/mi><mi>t<\/mi><mi>c<\/mi><mi>h<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5339<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u914d<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5185<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u6570<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7ec4<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5185<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7684<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5143<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7d20<\/mo><\/mrow><\/math>within \u8303\u56f4\u67e5\u8be2\uff08\u57fa\u4e8eLBS\uff09<br>\ud835\udc4f\ud835\udc5c\ud835\udc65\u8303\u56f4\u67e5\u8be2\uff0c\u77e9\u5f62\u8303\u56f4\uff08\u57fa\u4e8e\ud835\udc3f\ud835\udc35\ud835\udc46\uff09<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>b<\/mi><mi>o<\/mi><mi>x<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8303<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u56f4<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u67e5<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8be2<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff0c<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u77e9<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5f62<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8303<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u56f4<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff08<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u57fa<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e8e<\/mo><\/mrow><mi>L<\/mi><mi>B<\/mi><mi>S<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff09<\/mo><\/mrow><\/math>center \u8303\u56f4\u9192\u8be2\uff0c\u5706\u5f62\u8303\u56f4\uff08\u57fa\u4e8eLBS\uff09<br>\ud835\udc50\ud835\udc52\ud835\udc5b\ud835\udc61\ud835\udc52\ud835\udc5f\ud835\udc46\ud835\udc5d\u210e\ud835\udc52\ud835\udc5f\ud835\udc52\u8303\u56f4\u67e5\u8be2\uff0c\u7403\u5f62\u8303\u56f4\uff08\u57fa\u4e8e\ud835\udc3f\ud835\udc35\ud835\udc46\uff09<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi>c<\/mi><mi>e<\/mi><mi>n<\/mi><mi>t<\/mi><mi>e<\/mi><mi>r<\/mi><mi>S<\/mi><mi>p<\/mi><mi>h<\/mi><mi>e<\/mi><mi>r<\/mi><mi>e<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8303<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u56f4<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u67e5<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8be2<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff0c<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u7403<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u5f62<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u8303<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u56f4<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff08<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u57fa<\/mo><\/mrow><mrow class=\"MJX-TeXAtom-ORD\"><mo>\u4e8e<\/mo><\/mrow><mi>L<\/mi><mi>B<\/mi><mi>S<\/mi><mrow class=\"MJX-TeXAtom-ORD\"><mo>\uff09<\/mo><\/mrow><\/math>slice \u67e5\u8be2\u5b57\u6bb5\u96c6\u5408&nbsp;\u4e2d\u7684\u5143\u7d20\uff08\u6bd4\u5982\u4ece\u7b2c\u51e0\u4e2a\u4e4b\u540e\uff0c\u7b2cN\u5230\u7b2cM\u4e2a\u5143\u7d20<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><a><\/a>4.1.1&nbsp;find()<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code><code># conditions \u67e5\u8be2\u6761\u4ef6\u3001fields \u60f3\u8981\u67e5\u8be2\u7684\u5b57\u6bb5\u3001options \u3001callback \u56de\u8c03\u51fd\u6570\nModel.find(conditions, &#91;fields], &#91;options], &#91;callback]) \n#demo\nuserModel.find({'name':'\u5f20\u4e09'},{'name':1,'sex':1,'region':1,'createBy':1,'_id':0})\n\t\t.skip(1).limit(2).sort({'createBy.createTime' : -1})\n# \u67e5\u8be2\u6761\u4ef6:{'name':'\u5f20\u4e09'}\n# \u663e\u793a\u7684\u5b57\u6bb5:{'name':1,'sex':1,'region':1,'createBy':1,'_id':0}\n# \u4ece\u7b2c\u4e8c\u6761\u6570\u636e\u5f00\u59cb: .skip(1)\n# \u53d6\u4e24\u6761\u6570\u636e: .limit(2)\n# \u6392\u5e8f\u65b9\u5f0f: .sort({'createBy.createTime' : -1})\n\n# \u8fd8\u6709\u53e6\u4e00\u79cd\u65b9\u5f0f\nuserModel.find({'name':'\u5f20\u4e09'},\n\t\t\t{'name':1,'sex':1,'region':1,'createBy':1,'_id':0},\n\t\t\t{ limit:2, skip:1, sort:'-createBy.createTime'})\n# \u67e5\u8be2\u975e\u7a7a\u5b57\u6bb5\nModel.find(conditions:{$exists:true})\nModel.find(conditions:{$ne:null})\n\n# \u5206\u9875\u67e5\u8be2\nModel.find(conditions).skip(pageTotal * pageNum).limit(pageTotal).sort({'_id':-1}).exec(cb);\n\n# \u6a21\u7cca\u67e5\u8be2\nuserModel.find({\"name\" : {$regex:'\u5927\u867e'}})\nuserModel.find({\"name\" : \/\u5927\u867e\/ }})\n<\/code>AI\u5199\u4ee3\u7801js<img decoding=\"async\" src=\"https:\/\/csdnimg.cn\/release\/blogv2\/dist\/pc\/img\/runCode\/icon-arrowwhite.png\" alt=\"\">12345678910111213141516171819202122232425<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><a><\/a>4.1.2 where<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code><code>find({$where : \"this.name == 'a'\"})\nModel.$where('this.firstname === this.lastname').exec(callback)\n\nModel\n.where('age').gte(25)\n.where('tags').in(&#91;'movie', 'music', 'art'])\n.select('name', 'age', 'tags')\n.skip(20)\n.limit(10)\n.asc('age')<em>\/\/\u6392\u5e8f<\/em>\n.slaveOk()<em>\/\/\u5141\u8bb8\u6b64\u8fde\u63a5\u4ece\u526f\u672c\u5bf9\u7684\u975e\u4e3bmembr\u8bfb\u53d6<\/em>\n.hint({ age: 1, name: 1 }) <em>\/\/\u5f3a\u5236\u4f7f\u7528\u7d22\u5f15<\/em>\n.run(callback)\n<\/code><\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><a><\/a>4.1.3 findById()<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>Model.findById(conditions, &#91;fields], &#91;options], &#91;callback])\n# \u4e0e Model.find \u76f8\u540c\uff0c\u4f46\u5b83\u63a5\u6536\u6587\u6863\u7684 _id \u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u5355\u4e2a\u6587\u6863\u3002_id \u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u6216 ObjectId \u5bf9\u8c61<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><a><\/a>4.1.4 findOne()<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>Model.findOne(conditions, &#91;fields], &#91;options], &#91;callback])\n# \u4e0e Model.find \u76f8\u540c\uff0c\u4f46\u53ea\u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u7b2c\u4e00\u4e2a\u6587\u6863\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a><\/a><a><\/a>4.2 \u63d2\u5165<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><a><\/a>4.2.1&nbsp;model.create()<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>TestModel.create(\n\t{ candy: 'jelly bean' }, \n\t{ candy: 'snickers' },\n\t function (err, jellybean, snickers) {\n});\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><a><\/a>4.2.2 Model.collection.insert( )<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">insert( ) \u5728\u65b0\u7248\u672c\u5df2\u7ecf\u88ab\u66ff\u6362\u4e86\uff0c\u66ff\u6362\u6210insertOne()\u548cinsertMany()<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Model.collection.insert(docs, options, callback)\n# docs - \u8981\u63d2\u5165\u7684\u6587\u6863\u662f\u6570\u7ec4\n# options - \u53ef\u9009\u7684\u914d\u7f6e\u5bf9\u8c61- \u8bf7\u53c2\u89c1 docs\n# callback(err, docs) - \u4fdd\u5b58\u5b8c\u6240\u6709\u6587\u6863\u6216\u51fa\u73b0\u9519\u8bef\u540e\u8c03\u7528\uff0c\u83b7\u53d6\u4e4b\u540e\uff0c \u5c06\u8c03\u7528\u5426\u5219\u51fa\u9519\u3002 \u5982\u679c\u6210\u529f\uff0c docs \u662f\u6587\u6863\u7684\u4fdd\u5b58\u7684\u6570\u7ec4<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a><\/a><a><\/a>4.3 \u5220\u9664<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><a><\/a>4.3.1 \u5220\u9664\u67e5\u8be2\u5230\u7684\u7b2c\u4e00\u4e2a\u6587\u6863<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>Model.remove(conditions,callback);\nModel.deleteOne(conditions,callback);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><a><\/a>4.3.2 \u5220\u9664\u7b26\u5408\u6761\u4ef6\u7684\u6240\u6709\u6587\u6863<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>Model.deleteMany(conditions,callback);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a><\/a><a><\/a>4.4 \u6c47\u603b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Model.count(conditions,callback);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u5b89\u88c5 2.\u8fde\u63a5mongodb 3.\u521b\u5efa\u6a21\u578b \u8bbe..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[25,26],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-24","tag-mongodb","tag-mongose"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/%e6%95%b0%e6%8d%ae%e5%ba%93\/\" rel=\"category tag\">\u6570\u636e\u5e93<\/a>","tag_info":"\u6570\u636e\u5e93","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":2,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":338,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/336\/revisions\/338"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}