node.js 猫鼬字符串到 ObjectID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38446346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:25:06  来源:igfitidea点击:

Mongoose String to ObjectID

node.jsmongodbmongoose

提问by Melixion

i have string with ObjectId .

我有 ObjectId 的字符串。

var comments = new Schema({
    user_id:  { type: Schema.Types.ObjectId, ref: 'users',required: [true,'No user id found']},
    post: { type: Schema.Types.ObjectId, ref: 'posts',required: [true,'No post id found']}....

export let commentsModel: mongoose.Model<any> = mongoose.model("comments", comments);

How i user it:

我如何使用它:

let comment = new commentsModel;
str = 'Here my ObjectId code' //
comment.user_id = str;
comment.post = str;
comment.save();

When I create a "comment" model and assign a string user_id value or post I have an error when saving. I make console.log(comment)all data is assigned to vars.

当我创建一个“评论”模型并分配一个字符串 user_id 值或发布时,我在保存时出错。我将console.log(comment)所有数据分配给 vars。

I try:

我尝试:

 var str = '578df3efb618f5141202a196';
    mongoose.mongo.BSONPure.ObjectID.fromHexString(str);//1
    mongoose.mongo.Schema.ObjectId(str);//2
    mongoose.Types.ObjectId(str);//3
  1. TypeError: Object function ObjectID(id) {
  2. TypeError: Cannot call method 'ObjectId' of undefined
  3. TypeError: Cannot read property 'ObjectId' of undefined
  1. 类型错误:对象函数 ObjectID(id) {
  2. 类型错误:无法调用未定义的方法“ObjectId”
  3. 类型错误:无法读取未定义的属性“ObjectId”

And of course I included the mongoose BEFORE ALL CALLS

当然,我在所有通话之前都包括了猫鼬

import * as mongoose from 'mongoose';

nothing works.

没有任何效果。

回答by robertklep

You want to use the default export:

您想使用默认导出:

import mongoose from 'mongoose';

After that, mongoose.Types.ObjectIdwill work:

之后,mongoose.Types.ObjectId将工作:

import mongoose from 'mongoose';
console.log( mongoose.Types.ObjectId('578df3efb618f5141202a196') );

EDIT:full example (tested with [email protected]):

编辑:完整示例(用 测试[email protected]):

import mongoose from 'mongoose';

mongoose.connect('mongodb://localhost/test');

const Schema = mongoose.Schema;

var comments = new Schema({
    user_id:  { type: Schema.Types.ObjectId, ref: 'users',required: [true,'No user id found']},
    post: { type: Schema.Types.ObjectId, ref: 'posts',required: [true,'No post id found']}
});

const commentsModel = mongoose.model("comments", comments);

let comment = new commentsModel;
let str = '578df3efb618f5141202a196';
comment.user_id = str;
comment.post = str;
comment.save().then(() => console.log('saved'))
              .catch(e => console.log('Error', e));

Database shows this:

数据库显示:

mb:test$ db.comments.find().pretty()
{
    "_id" : ObjectId("578e5cbd5b080fbfb7bed3d0"),
    "post" : ObjectId("578df3efb618f5141202a196"),
    "user_id" : ObjectId("578df3efb618f5141202a196"),
    "__v" : 0
}

回答by Azeem Malik

use this

用这个

 var mongoose = require('mongoose');
 var str = '578df3efb618f5141202a196';
 var mongoObjectId = mongoose.Types.ObjectId(str);