Query.prototype.map() 函數用於運行函數 fn 並將 fn 的返回值視為要解析為查詢的新值。傳遞給 map() 的所有函數函數將在任何 post hook 之後運行。
用法:
Query.prototype.map()
參數:該函數有一個參數fn,它是運行以轉換查詢結果的函數。返回值:該函數返回查詢對象。
Mongoose Installation:
npm install mongoose
安裝 mongoose 模塊後,您可以使用命令在命令提示符下檢查您的 mongoose 版本。
npm mongoose --version
之後,您可以創建一個文件夾並添加一個文件,例如 index.js,如下所示。
數據庫:這裏使用的示例數據庫如下所示:
範例1:
這裏,文件名是 index.js
Javascript
const mongoose = require('mongoose');
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
useNewUrlParser:true,
useCreateIndex:true,
useUnifiedTopology:true
});
// User model
const User = mongoose.model('User', {
name:{ type:String },
age:{ type:Number }
});
const query = User.find({name:"Lalit"}).map(res => {
console.log("loadedAt property set on the doc "
+ "to tell the time doc was loaded.")
return res == null ? res:Object.assign(res,
{ loadedAt:new Date() });
});
query.exec(function(err, res){
if(err) console.log(err)
else console.log(res)
});
項目結構將如下所示:
使用以下命令運行index.js文件:
node index.js
輸出:
loadedAt property set on the doc to tell the time doc was loaded. [ { _id:5ebc3669a99bde77b2efb9ba, name:'Lalit', age:25, __v:0 }, loadedAt:2020-07-14T18:22:57.991Z ]
範例2:
這裏,文件名是 index.js
Javascript
const express = require('express');
const mongoose = require('mongoose');
const app = express()
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
useNewUrlParser:true,
useCreateIndex:true,
useUnifiedTopology:true
});
// User model
const User = mongoose.model('User', {
name:{ type:String },
age:{ type:Number }
});
const query = User.find().map(res => {
console.log("loadedAt property set on the doc "
+ "to tell the time doc was loaded.")
return res == null ? res:Object.assign(res,
{ loadedAt:new Date() });
});
query.exec(function(err, res){
if(err) console.log(err)
else console.log(res)
});
app.listen(3000, function(error ) {
if(error) console.log(error)
console.log("Server listening on PORT 3000")
});
項目結構將如下所示:
使用以下命令運行index.js文件:
node index.js
輸出:
Server listening on PORT 3000 Server listening on PORT 3000 loadedAt property set on the doc to tell the time doc was loaded. [ { _id:5ebb9129a99bde77b2efb809, name:'Gourav', age:10, __v:0 }, { _id:5ebc3669a99bde77b2efb9ba, name:'Lalit', age:25, __v:0 }, { _id:5ebc367da99bde77b2efb9bf, name:'Piyush', age:5, __v:0 }, { _id:5ebd345f5d2d8a3534b2f391, name:'Manish', age:34, __v:0 }, loadedAt:2020-07-14T18:24:36.890Z ]
參考: https://mongoosejs.com/docs/api/query.html#query_Query-map
相關用法
- Mongoose Query.prototype.circle()用法及代碼示例
- Mongoose Query.prototype.maxTimeMS()用法及代碼示例
- Mongoose Query.prototype.merge()用法及代碼示例
- Mongoose Query.prototype.lte()用法及代碼示例
- Mongoose Query.prototype.getPopulatedPaths()用法及代碼示例
- Mongoose Query.prototype.geometry()用法及代碼示例
- Mongoose Query.prototype.gte()用法及代碼示例
- Mongoose Query.prototype.hint()用法及代碼示例
- Mongoose Query.prototype.mod()用法及代碼示例
- Mongoose Query.prototype.getUpdate()用法及代碼示例
- Mongoose Query.prototype.centerSphere()用法及代碼示例
- Mongoose Query.prototype.explain()用法及代碼示例
- Mongoose Query.prototype.exec()用法及代碼示例
- Mongoose Query.prototype.error()用法及代碼示例
- Mongoose Query.prototype.equals()用法及代碼示例
- Mongoose Query.prototype.elemMatch()用法及代碼示例
- Mongoose Query.prototype.distinct()用法及代碼示例
- Mongoose Query.prototype.cursor()用法及代碼示例
- Mongoose Query.prototype.comment()用法及代碼示例
- Mongoose Query.prototype.catch()用法及代碼示例
- Mongoose Query.prototype.collation()用法及代碼示例
注:本文由純淨天空篩選整理自gouravhammad大神的英文原創作品 How does Query.prototype.map() works in Mongoose ?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。