Code keep inserting empty values

Can anyone help me why after I insert a record into collection there tons of empty records also being inserted right after that. I don’t know what did I do wrong. Here is the picture


and here is my mongoose model

(function () {

  var mongoose = require("mongoose"),

    FileSchema = mongoose.Schema({

      url: {

        type: String,

        maxlength: 256,

      },

      filename: {

        type: String,

        maxlength: 128,

      },

      fileType: {

        type: String,

        maxlength: 50,

      },

    }),

    CreateBySchema = mongoose.Schema({

      userId: {

        type: String,

        maxlength: 24,

      },

      avatar: {

        type: String,

        maxlength: 256,

      },

      username: {

        type: String,

        maxlength: 50,

      },

    }),

    ChargeSchema = mongoose.Schema({

      id: {

        type: String,

        maxlength: 50,

      },

      amount: Number,

      description: {

        type: String,

        maxlength: 100,

      },

      created: Number,

    }),

    itemSchema = mongoose.Schema({

      title: {

        type: String,

        maxlength: 100,

      },

      businessName: {

        type: String,

        maxlength: 50,

      },

      files: {

        type: [FileSchema],

        default: undefined,

      },

      modifiedDate: {

        type: Date        

      },

      createdBy: CreateBySchema,

      tags: {

        type: [

          {

            type: String,

            maxlength: 30,

          },

        ],

        validate: [arrayLimit, "{PATH} exceeds the limit of 5"],

        default: undefined,

      },

      needs: {

        type: [

          {

            type: String,

            maxlength: 30,

          },

        ],

        validate: [arrayLimit, "{PATH} exceeds the limit of 5"],

        default: undefined,

      },

      wage: Number,

      categories: {

        type: [

          {

            type: String,

            maxlength: 50,

          },

        ],

        default: undefined,

      },

      noOfPoints: Number,

      noOfComments: Number,

      noOfViews: Number,

      hasUpvoted: Boolean,

      hasReported: Boolean,

      price: Number,

      address: {

        type: String,

        maxlength: 100,

      },

      address2: {

        type: String,

        maxlength: 50,

      },

      zipcode: {

        type: String,

        maxlength: 10,

      },

      city: {

        type: String,

        maxlength: 40,

      },

      state: {

        type: String,

        maxlength: 20,

      },

      country: {

        type: String,

        maxlength: 20,

      },

      noOfEmployees: Number,

      noOfChairs: Number,

      noOfTables: Number,

      contactPhoneNo: {

        type: String,

        maxlength: 18,

      },

      contactEmail: {

        type: String,

        maxlength: 50,

      },

      income: Number,

      rentCost: Number,

      otherCost: Number,

      leaseEnd: Date,

      yearOld: Number,

      area: Number,

      duration: Number,

      overview: {

        type: String,

        maxlength: 180,

      },

      description: {

        type: String,

        maxlength: 2000,

      },

      charge: ChargeSchema,

      status: {

        type: String,

        maxlength: 20,

      },

      geometry: Object,

      expired: {

        type: Boolean,

      },

      refundable: {

        type: Boolean,

      },

      coupon: {

        appliedCoupon: Boolean,

        discount: Number,

      },

      isSpecial: {

        type: Boolean,

      },

    });

  function arrayLimit(val) {

    return val.length <= 5;

  }

  itemSchema.index({ modifiedDate: -1, tags: 1 });

  //TTL of modifiedDate.

  //157680000 is 5years , 94608000 is 3 years, 63072000 is 2 years , 34190000 is 13 months

  module.exports = mongoose.model("item", itemSchema);

})();

Your title says Mongo keep inserting empty values but I would be very surprised if Mongo does that.
More likely it is an issue in your code. You shared your mongoose schema but it is of no use for us to help you. We need to see your code where you

We also need so idea about the architecture of your application. Perhaps you have to other process running creating the extra documents.

Hi @steevej , thanks for helping me, here is my insert function

  router.post("/svc/business/create", middleware.isValidUser, (req, res) => {
    var item = req.body;
    validate(req.user, item)
      .then((isValid) => {
        if (!isValid) {
          return res.status(status.INTERNAL_SERVER_ERROR).json("Invalid form");
        }
        return itemSvc.addItem(item);
      })
      .then((newItem) => {
        return res.status(status.OK).json(newItem);
      })
      .catch((err) => {
        // console.log("Business/create err: ");
        // console.log(err);
        return res.status(status.INTERNAL_SERVER_ERROR).json(err);
      });
  });


async function validate(user, item) {
    let charge = item.charge;
    let price = 2000;
    if (item.coupon && item.coupon.appliedCoupon) {
      //make sure discount is correct
      if (item.coupon.discount !== 0.5) {
        return false;
      }
      price = price * item.coupon.discount;
    }
    let duration = Number(item.duration);
    if (user.role !== "ADMIN" && duration === 0.5) {
      let existItem = await itemSvc.getOneItem({
        "createdBy.userId": user.id,
        duration: 0.5,
      });
      if (existItem) {
        return false;
      }
    }
    if (duration === 1 && charge.amount !== price) {
      return false;
    }
    if (duration === 3 && charge.amount !== price * 2) {
      return false;
    }
    if (duration === 6 && charge.amount !== price * 3) {
      return false;
    }
    //duration
    if (duration === 24 && charge.amount !== price * 4) {
      return false;
    }
    if (
      duration !== 0.5 &&
      duration !== 1 &&
      duration !== 3 &&
      duration !== 6 &&
      duration !== 24
    ) {
      return false;
    }
    if (
      !item.files ||
      item.files.length > 10 ||
      item.files.length <= 0 ||
      (item.tags && item.tags.length > 5) ||
      (item.categories && item.categories.length > 20)
    ) {
      return false;
    }
    item.tags = item.tags.slice(0, 5);
    item.createdBy = {
      userId: user.id,
      avatar: user.avatar,
      username: user.username,
      rank: user.rank,
      noOfFollowers: user.noOfFollowers,
    };
    item.modifiedDate = moment().format();
    item.noOfPoints = 0;
    item.noOfSeens = 0;
    item.noOfShares = 0;
    item.noOfComments = 0;
    item.status = "NEW";
    return true;
  }

//item services 
  function addItem(item) {
    return new Promise((resolve, reject) => {
      Item.create(item, (err, item) => {
        if (err) {
          return reject(err);
        }
        return resolve(item);
      });
    });
  }

I deployed all the functions to firebase.

Also i have this code running to get random sample of items. Wonder if this could cause the issue

  function getRandomItems(conditions) {
    return new Promise((resolve, reject) => {
      Item.aggregate()
        .match(conditions)
        .sample(50)
        .exec((err, items) => {
          if (err) {
            return reject(err);
          } else {
            return resolve(
              items.filter((item) => {
                return !isExpired(item);
              })
            );
          }
        });
    });

Nothing obvious in that.

Do you have some code that counts the number of times the item is viewed? A GET route or something like that.

Your first post had a screenshot that indicates that your document had 13 more fields. Can you share those 13 fields?

@steevej OMG yes, i have that, that could be the problem
here is the code

  router.post("/svc/business/:id/upview", (req, res) => {
    var condition = {
      _id: req.params.id,
    };
    itemSvc
      .getItemByIdAndIncreaseView(condition)
      .then((item) => {
        return processOne(req, res, item);
      })
      .then((result) => {
        return res.status(status.OK).json(result);
      })
      .catch((err) => {
        return res.status(status.INTERNAL_SERVER_ERROR).json(err);
      });
  });

  function getItemByIdAndIncreaseView(item) {
    return updateItem(
      item,
      { $inc: { noOfViews: 1 } },
      { upsert: true, new: true }
    );
  }

  function updateItem(conditions, newInfo, options) {
    return new Promise((resolve, reject) => {
      Item.findOneAndUpdate(conditions, newInfo, options, (err, newItem) => {
        if (err) {
          return reject(err);
        }
        return resolve(newItem);
      });
    });
  }

Your bug is in

and

You receive req.params.id as a string but it is stored as an ObjectId. So when you updateItem() no item is ever found. But upsert is true so a new item is created with noOfViews:1 as the only field. I am also not too sure if new:true is a valid option.

3 Likes

OMG thank you so much for pointing this out. I’m new to Mongo so I am really stuck here. Once again thanks so much

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.