标签: mongoose-schema

尝试以变量为键查询猫鼬

如何使用变量作为我要搜索的键来查询 mongodb?

数据:

const schedule = {
  day0: [10, 1440],
  day1: [10, 1440],
  day3: [10, 1440],
  day6: [10, 1440],
}
Run Code Online (Sandbox Code Playgroud)

查询

User.find({ `schedule.${varHere}` { $exists: true}}, (err, users) => {
  console.log(users)
})
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongoose-schema

2
推荐指数
1
解决办法
2405
查看次数

猫鼬填充不适用于 ObjectIds 数组

这是我的架构:

/** Schemas */
var profile = Schema({
    EmailAddress: String,
    FirstName: String,
    LastName: String,
    BusinessName: String
});

var convSchema = Schema({
    name: String,
    users: [{
        type: Schema.Types.ObjectId,
        ref: 'Profiles'
    }],
    conversationType: {
        type: String,
        enum: ['single', 'group'],
        default: 'single'
    },
    created: {
        type: Date,
        default: Date.now
    },
    lastUpdated: {
        type: Date,
        default: Date.now
    }
});

/** Models */
db.Profiles = mongoose.model('Profiles', profile);
db.Conversations = mongoose.model('ChatConversations', convSchema);

module.exports = db;
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用以下代码(http://mongoosejs.com/docs/populate.html)填充用户:

db.Conversations.find(query).populate('users').exec(function (err, records)     {
    console.log(records);
});
Run Code Online (Sandbox Code Playgroud)

这是返回records …

mongoose mongodb node.js mongoose-populate mongoose-schema

2
推荐指数
1
解决办法
3122
查看次数

如何处理 GraphQL Schema 定义中的连字符

我的猫鼬模式如下

var ImageFormats = new Schema({
     svg        : String,
     png-xlarge : String,
     png-small  : String
});
Run Code Online (Sandbox Code Playgroud)

当我将其翻译成 GraphQL 模式时,这就是我尝试的

export var GQImageFormatsType: ObjectType = new ObjectType({
     name: 'ImageFormats',

     fields: {
          svg        : { type: GraphQLString },
         'png-xlarge': { type: GraphQLString },
         'png-small' : { type: GraphQLString }
 }
});
Run Code Online (Sandbox Code Playgroud)

GraphQL 返回以下错误: Error: Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "png-xlarge" does not.

如果我尝试在 Mongoose 模型之后对 GraphQL 进行建模,我该如何协调这些字段?有没有办法让我创建别名?

(我在 graffiti 和 stackoverflow 论坛上搜索过这个,但找不到类似的问题)

mongoose mongodb mongoose-schema graphql graphql-js

2
推荐指数
1
解决办法
3413
查看次数

如何在猫鼬中更新参考数组

我有一个组集合,它有一个成员的参考数组。两个对象像下面一样相互连接。当我向组添加新成员时,需要更新 Group 对象的 members 字段。我怎么能用mongoose update操作员做到这一点。

var MemberSchema = new Schema({
    name:{
        type:String,
        default:null
    },
    user_id:{
        type : Schema.ObjectId,
        ref : 'User',
        default : null
    }
});

var GroupSchema = new Schema({
    name:{
        type:String,
        default:null
    },
    description:{
        type:String,
        default:null
    },
    members:[MemberSchema],

},{collection:"groups"});
Run Code Online (Sandbox Code Playgroud)

先感谢您。

更新

我添加了一个组的示例文档。

{
    "_id" : ObjectId("586a2e694467c41218b302c3"),
    "members" : [ 
        {
            "_id" : ObjectId("586a2e694467c41218b302c6"),
            "user_id" : ObjectId("58171d75e72bf516f92dcd4e"),
            "name" : "Lakmal Kapukotuwa"
        }, 
        {
            "_id" : ObjectId("586a2e694467c41218b302c5"),
            "user_id" : ObjectId("5821807516325e127f59438e"),
            "name" : "Prasad Perera"
        }, 
        {
            "_id" …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongoose-schema

2
推荐指数
1
解决办法
6183
查看次数

Mongoose getter 和 setter 无法正常工作

我正在尝试在插入 MongoDB 之前和之后加密和解密值。我正在使用猫鼬模式并调用 get 和 set 方法进行加密和解密。通过调用 set 方法对数据进行加密,但是在从 MongoDB 检索数据时它没有解密。这是我的架构和加密解密方法:

var tempSchema = new Schema({    
  _id: {type: Schema.ObjectId, auto: true},
  name:{type: String},
  sample_details:{
    _id: false,
    objects: [{
      object_key:{type: String},
      object_value:{type: String, get:decrypt, set:encrypt}
    }],
    type_params:[{
      type_key:{type: String},
      type_value:{type: String, get:decrypt, set:encrypt}
    }],
    test_body:{type: String, get:decrypt, set:encrypt}
  }}, {
  toObject : {getters: true, setters: true},
  toJSON : {getters: true, setters: true}
});
Run Code Online (Sandbox Code Playgroud)

以下是使用的加密和解密方法:

function encrypt(text){
     var cipher = crypto.createCipher('aes-256-cbc', secret_key);
     var crypted = cipher.update(text,'utf8','hex');
     crypted += cipher.final('hex');
     return crypted;
} …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js mongoose-schema node-crypto

2
推荐指数
1
解决办法
4563
查看次数

需要 <script> node.js 中的函数

我可以在脚本标签中放置一个 require 函数吗,例如如下所示:

<script>
    var User = require('../models/user');
    alert('It is working');
</script>
Run Code Online (Sandbox Code Playgroud)

上面的代码是我的layout.handlebar代码。但似乎不起作用。因为我需要获取此脚本,以便我可以访问车把模板引擎上的 user.js 脚本。

用户.js

var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');

//User Schema
var UserSchema = mongoose.Schema({
username:{
    type: String,
    index:true
},
password:{
    type:String
},
email:{
    type:String
},
name:{
    type:String
},
field:{
    type:String
},
e_money:{
    type:Number //this is the integer form in mongoose
}
});

//accesible variable from the outside
var User = module.exports = mongoose.model('User', UserSchema);

//create the user
module.exports.createUser= function(newUser, callback){
bcrypt.genSalt(10, function(err,salt){
bcrypt.hash(newUser.password, salt, …
Run Code Online (Sandbox Code Playgroud)

javascript node.js handlebars.js mongoose-schema

2
推荐指数
1
解决办法
3113
查看次数

mongoose 中的 moment js 没有更新

所以我在 momentjs 上遇到了麻烦。当我拨打电话后,时间戳没有更新。我等了 30 秒才再次拨打电话,但仍然得到相同的时间。我怎样才能解决这个问题?下面是我的代码,所以让我知道我做错了什么。

var mongoose = require('mongoose');
var moment = require('moment');
var now = moment();

var UserSchema = new mongoose.Schema({
    email: { type: String, default: ''},
    name: { type: String, default: ''},
    password: { type: String, default: ''},
    timestamp: {type: String, default: now.format("dddd, MMMM Do YYYY, h:mm:ss a")}
});

module.exports = mongoose.model('UserSchema', UserSchema);

module.exports = {
    find: function(params, callback) {
        User.find(params, function(err, result) {
            if (err) {
                callback(err, null);
                return;
            }
            callback(null, result);
            return;
        });
    },
    create: function(params, …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb momentjs mongoose-schema

2
推荐指数
1
解决办法
4168
查看次数

如何查找值不在 Mongoose 数组字段中的文档?

在 Mongoose 中,如果模型 M 具有此字段:

list: {type:[String]}
Run Code Online (Sandbox Code Playgroud)

我应该如何找到一个特定值 x 不是“列表”元素的文档?我希望有一个特殊的运算符“$ncontains”,以便我可以执行以下操作:

M.findOne({list:{$ncontains:x}}...
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongodb-query mongoose-schema

2
推荐指数
1
解决办法
5956
查看次数

猫鼬在 pre('save') 钩子中调用 .find

目前我正在尝试执行以下操作:

const ItemSchema = mongoose.Schema({
 ...
 name : String
 ...
});

ItemSchema.pre('save', async function() {
  try{
     let count = await ItemSchema.find({name : item.name}).count().exec();
     ....
     return Promise.resolve();
  }catch(err){
     return Promise.reject(err)
  }
});

module.exports = mongoose.model('Item', ItemSchema);
Run Code Online (Sandbox Code Playgroud)

但我只是收到以下错误:

类型错误:ItemSchema.find 不是函数

如何在我的 post('save') 中间件中调用 .find() 方法?(我知道,Schmemas 上有一个独特的属性。如果名称字符串已经存在,我必须以这种方式对其进行后缀)

猫鼬版本:5.1.3 nodejs 版本:8.1.1 系统:ubuntu 16.04

mongoose node.js mongoose-schema

2
推荐指数
1
解决办法
1477
查看次数

文章的猫鼬模式

我正在建立一个新闻网站,我有这个猫鼬模式:

let mongoose = require('mongoose');

let articleSchema = mongoose.Schema({
  image1:{
    type: String,
    required: true
  },
  title:{
    type: String,
    required: true
  },
  author:{
    type: String,
    required: true
  },
  date:{
    type: String,
    required: true
  },
  updated:{
    type: String,
    default: 'not updated'
  },
  title_nd:{
    type: String,
    required: false
  },
  body:{
    type: String,
    required: true
  },
  comments: [commentsSchema],
  likes:{ type:Number, default:0 }
});

let Article = module.exports = mongoose.model('Article', articleSchema);

Run Code Online (Sandbox Code Playgroud)

我想添加一个表单,以便用户可以添加他们的评论。问题是如何为评论创建新架构并将其链接到文章架构,然后如果用户添加评论,则评论添加到数据库,然后显示在文章评论部分?

mongoose mongodb node.js mongoose-schema pug

2
推荐指数
1
解决办法
710
查看次数