获取插入的文档nodejs和mangodb的ID

问题描述

我尝试从nodejs插入到mangodb,我可以将文档插入数据库,但是我不知道如何获取ID,插入时可以获取ID成功吗?

这是insert.js

const Video = require("../models/Video");

Video.findOne({ title },(err,user) => {
    if (err) {
      res
        .status(500)
        .json({ message: { msgBody: "Error hac occured",msgError: true } });
    }
    if (user) {
      res.status(400).json({
        message: { msgBody: "Title is already taken",msgError: true },});
    } else {
      const newvideo = new Video({ title,singer,country,genre });
      newvideo.save((err) => {
        if (err) {
          res.status(500).json({
            message: { msgBody: "Error has occured",});
        } else {
          res.status(201).json({
            message: { msgBody: "Add success",msgError: false },});
        }
      });
    }
  });

这是Video.js

const mongoose = require("mongoose");

const VideoSchema = new mongoose.Schema({
  title: {
    type: String,required: true,},singer: {
    type: String,country: {
    type: String,genre: {
    type: String,});

module.exports = mongoose.model("video",VideoSchema);

希望你们明白我在问什么:D

解决方法

save方法还在回调中提供响应。请检查以下代码:

def printPerfectNumbers(firstNum,lastNum):
 
   for i in range(firstNum,lastNum +1):
       totalFactors = 0
       for x in range(1,i):
       # Checks if x is a divisor,if true,adds x to totalFactors.
           if(i % x == 0):
               totalFactors = totalFactors + x
       if (totalFactors == i):
           print(i,end = " ")

printPerfectNumbers(1,1000)