我正在使用貓鼬查詢本地資料庫并嘗試過:(假設cardName已定義)
const card = await Card.findOne({ name: cardName }, 'name art description')
const card = await Card.findOne({ name: cardName }, { name: 1,art: 1,description: 1 })
const card = await Card.findOne({ name: cardName }).project('name art description')
const card = await Card.findOne({ name: cardName }, 'name art description').exec()
即使我得到了我指定的欄位,我也得到了其他不必要的欄位,例如
$__schema: [object Object]
collection: [object Object]
$collection: [object Object]
$__originalValidate: function(pathsToValidate, options, callback) { if (typeof pathsToValid . . .
$__save: function() { var _context = context || this; _this.wrap(name, fn, _context, Array.from(arguments), options); }
$__validate: function() { var _context = context || this; _this.wrap(name, fn, _context, Array.from(arguments), options); }
$__remove: function() { var _context = context || this; _this.wrap(name, fn, _context, Array.from(arguments), options); }
$__deleteOne: function() { process.nextTick(() => fn.apply(this, arguments)); }
$__init: function syncWrapper() { kareem.execPreSync(name, this, arguments); var toReturn = fn.apply(this, arguments); kareem.execPostSync(name, this, [toReturn]); return toReturn; }
$isMongooseModelPrototype: true
$__handleSave: function(options, callback) . . .
如何確保我只獲得我指定的欄位而不是任何不必要的資訊?
uj5u.com熱心網友回復:
您可以lean()在查詢中使用該功能。從檔案:
從啟用了精簡選項的查詢回傳的檔案是純 JavaScript 物件,而不是 Mongoose 檔案。它們沒有保存方法、getter/setter、virtuals 或其他 Mongoose 功能。
const docs = await Model.find().lean();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/430788.html
標籤:javascript 节点.js mongodb 猫鼬
上一篇:無法在M1Macbook上使用SQLite組合Docker容器
下一篇:有沒有辦法巧妙地撰寫下面的代碼?
