简体   繁体   English

在猫鼬中集合引用是如何工作的?

[英]How collection referencing works in mongoose?

First time i am trying to use refrence in mongoose , so i am trying to understand if i want to save template with user id , Do we need to get createdBy value from client or how it will be inserted into templateSchema . 第一次我尝试在猫鼬中使用Refreence,因此我试图了解是否要保存带有用户ID的模板,我们是否需要从客户端获取createdBy值或如何将其插入templateSchema basically i want to save user id _id when user save template.I was able to save template but i did not get any error and createdBy property did not saved to template collection. 基本上我想在保存用户模板时保存用户ID _id 。我能够保存模板,但是没有出现任何错误,而且createdBy属性没有保存到模板集合中。 Any layman explanation to understand refrencing in mongoose and how i can make it work with below code. 任何外行人的解释,以了解猫鼬的递归,以及我如何使其与下面的代码一起工作。

user.js user.js的

var UserSchema = new mongoose.Schema({
  _id: { type: String, required: true, index: {unique: true}},
  firstName: String,
  lastName: String,
  type: String,
  groups:[{type: String, ref: 'Group', required: false}]
},
  {
    toObject: {
      virtuals: true
    },
    toJSON: {
      virtuals: true
    }
  });

template.js template.js

var User = require('../user/user.model.js');

var TemplateSchema = new mongoose.Schema({
  _id: { type: String, required: true},
  name: String,
  id: String,
  appliesTo: [],
  properties: [],
  createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User'}
 });

templateCtrl.js templateCtrl.js

var eTemplate = require('./template.model');

var templatesJavaClass = {
          "_id": 12586,
          "name": "Java Class",
          "id": "javaClass",
          "appliesTo": [
              "bpmn:UserTask"
          ],
          "properties": [{
              "label": "Java Package Name",
              "type": "String",
              "editable": true,
              "binding": {
                  "type": "property",
                  "name": "camunda:class"
              }
          }],
        "createdBy": "user1"
      }

 var template = new eTemplate(templatesJavaClass);

 template.save(function(error){
  console.log("successfully saved template");
  if (error){
    console.log(error);
  }
 });

You need to put user _id in createdBy field of template document while creating/saving it. 创建/保存template时,需要将用户_id放在template文档的createdBy字段中。

Also, make sure it is of Type ObjectId and not string . 另外,确保它的类型为ObjectId而不是string Otherwise you might get Cast Error . 否则,您可能会得到Cast Error

Try this: 尝试这个:

var templatesJavaClass = {
    "_id": 12586,
    "name": "Java Class",
    "id": "javaClass",
    "appliesTo": [
        "bpmn:UserTask"
    ],
    "properties": [{
        "label": "Java Package Name",
        "type": "String",
        "editable": true,
        "binding": {
            "type": "property",
            "name": "camunda:class"
        }
    }],
  //save users _id in createdBy field
  "createdBy": user1._id;//assuming user1 is the users document
}

var template = new eTemplate(templatesJavaClass);

template.save(function(error){
    console.log("successfully saved template");
    if (error){
      console.log(error);
    }
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM