如何从我的MongoDB查询常量中获取值 [英] How to get the values from my MongoDB query a const
本文介绍了如何从我的MongoDB查询常量中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个MongoDB Schema调用设置,该模式具有一些值,我希望每个值都可以在另一个模式中用作常量
Let's say I have a MongoDB Schema call settings, this schema has some values that I want each of the value to be used in another schema as a constant
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const SettingSchema = new Schema({
dimension:{
type: Number
}
paystack:{
type: String
}
paypal:{
type: String
}
stripe:{
type: String
}
});
module.exports = Settings = mongoose.model('settings', SettingSchema);
{
"_id": "5d7f77d620cf10054ded50bb",
"dimension": 166,
"paystack": "A1212asdvsdvdd12121313",
"paypal": "Bfwfw2ewrewrr32ASDdd133",
"stripe": "Fr1aEacstewrr32ASDdd133",
"__v": 0
}
我要实现的目标是创建一个const维,该维将从我的设置架构中拉出166.
What I am trying to achieve is to create a const dimension that will pull out that 166 from my settings schema.
推荐答案
您可以尝试以下代码.
const settingModel = require('../models/setting.model');
const ObjectId = require('mongodb').ObjectId;
settingModel.aggregate([
{
$match: { '_id': ObjectId(5d7f77d620cf10054ded50bb) }
},
]).exec((err , data) =>{
if(err){
console.log(err);
}else{
console.log(data);
console.log(data[0].dimension);
}
这篇关于如何从我的MongoDB查询常量中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文