I dont know why this happens, I have a schema with a unique key for the field "address" but I get duplicate entries. I also check before I insert a new document if Model.exists() and it still inserts documents. I have no idea why, also I am getting occasional duplicate entry errors in the console. This is my code
const Schema = mongoose.Schema(
{
address : { type: String, unique: true },
isContract : { type: Boolean, required: true, default: false },
ethervalue : { type: Number, default: 0 },
symbol : { type: String, unique: true },
tokenname : { type: String},
divisor : { type: Number },
tokentype : {type: String},
bluecheckmark: {type : Boolean, default: false},
description: {type: String},
totalsupply: {type: Number},
},
{ timestamps: true }
);
async saveAddress(address) {
try {
const exists = await Address.exists({ address: address });
if (!exists) {
const isContract = await this.isContract(address);
let temp = new Address();
if (isContract) {
const info = await etherscan.getTokenInfoByContractAddress(address);
temp.isContract = true;
if(info.status == 1){
temp.symbol = info.result[0].symbol;
temp.tokenname = info.result[0].tokenName;
temp.totalsupply = info.result[0].totalSupply;
temp.divisor = info.result[0].divisor;
temp.tokentype = info.result[0].tokenType;
temp.description = info.result[0].description
temp.bluecheckmark = info.result[0].blueCheckmark
}
}
temp.address = address;
await temp.save();
}
} catch (error) {
console.log('saveAddres()', error.message);
}
}
To prevent duplicate entries in mongodb, you can add a unique index on that particular field. In this case: db.collection.createIndex({"address":1},{unique:true}) .
Refer: https://docs.mongodb.com/manual/core/index-unique/
Related
In my database, every article has its own 6-digit identifying number (in addition to id). Every comment belongs to one of the articles. My question is, how to query comments knowing only the article number, but not the article's id?
controllers/fetch-comments.js:
const Article = require('../models/article')
const User = require('../models/user');
const Comment = require('../models/comment');
module.exports = async function (req, res) {
try {
let { article_number, articleId } = req.body
let filter;
console.log(article_number)
/* Here is my failed attempt: */
if (article_number) filter = { 'article.number': article_number };
if (articleId) filter = { 'article': articleId };
let projection = null
let options = null
let comments = await Comment.find(filter, projection, options).populate('author', 'username fullname').lean()
return res.status(200).send(comments)
} catch (error) {
console.log("error on getting comments: " + error.message);
return res.status(500).send('Server side error');
}
}
models/article.js:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const articleSchema = new Schema({
author: { type: Schema.Types.ObjectId, ref: 'User', required: true },
title: { type: String, required: true },
content: { type: String, required: true },
coverPhotoUrl: { type: String, required: false },
number: { type: Number, required: true }, // number is public id for address bar
createdAt: { type: Date, required: true },
category: { type: String, required: false },
fake: { type: Boolean, required: false },
});
module.exports = mongoose.model('Article', articleSchema);
models/comment.js:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const commentSchema = new Schema({
author: { type: Schema.Types.ObjectId, ref: 'User', required: true },
content: { type: String, required: true },
createdAt: { type: Date, required: true },
article: { type: Schema.Types.ObjectId, ref: 'Article', required: true }
});
module.exports = mongoose.model('Comment', commentSchema);
Please do not suggest doing 2 queries intead of one, I already know how to do that.
You can use the aggregation framework and use a $match and $lookup operator to get the comment of a particular article number.
Following would be the code for the same:
db.Article.aggregate( [
{ $match : { number : article_number} },
{
$lookup:
{
from: "Comment",
localField: "_id",
foreignField: "article",
as: "article_comments"
}
}
] )
This will return an array of the matching comments.
The code is show below.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const user = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
resetToken: String,
resetExpiration: String,
products: [{type: mongoose.Types.ObjectId, required: true, ref: 'Shop'}],
cart: {
items: [
{
productId: {type: mongoose.Types.ObjectId, ref: 'Shop', required: true},
quantity: {type: Number, required: true},
}
]
},
});
user.methods.addToCart = (product) => {
const itemIndex = this.cart.items.findIndex(prod => {
return prod.productId.toString() === product._id.toString();
});
let newQuantity = 1;
const updatedCartItems = [...this.cart.items];
if(itemIndex >= 0) {
newQuantity = this.cart.items[itemIndex].quantity + 1;
updatedCartItems[itemIndex].quantity = newQuantity;
} else {
updatedCartItems.push({
productId: product,
quantity: newQuantity
});
}
const updatedCart = {
items: updatedCartItems
}
this.cart = updatedCart;
return this.save();
}
const model = mongoose.model('User', user);
module.exports = model;
I am trying to store product in the cart instance method as per above schema, but when i send product from my controller to addToCart it says items is undefined on this.cart.items. I haven't used instance method much in mongoose so, i don't know this issue is it with schema or general problem.
let me know if you need any other information.
It was a silly mistake, actually i was using arrow function. so it wasn't bind to schema.
In this application, I am trying to update a field value, if it is successful then I want to save into a log collection, however, not all the data are saved in the log collection, I am not sure if I am doing it the right way, would appreciate if someone could help out.
here is the query :
// both models(Log & Inventory are imported)
router.get("/add_product/:id/:num/:quantity/:order", (req, res) => {
var id = req.params.id;
var quantity = req.params.quantity;
var order = req.params.order;
// console.log('id----', id);
var num_mod = req.params.num;
var modified_count = parseInt(num_mod) - parseInt(quantity);
console.log("num_mod----", num_mod);
Inventory.findByIdAndUpdate(id, { quantity: parseInt(num_mod) }, { new: true }, function(
err,
inventory
) {
if (err) {
console.log("err", err);
res.status(500).send(err);
} else {
console.log(inventory.name);
const newLog = new Log({
name: inventory.name,
description: inventory.description,
price: parseInt(inventory.price),
quantity: parseInt(inventory.quantity),
modified_quantity: parseInt(modified_count),
itemDest: order //this is not being saved
});
newLog.save(function(err, Log) {
if (err) {
console.log(err);
} else {
console.log("add log success");
res.send(inventory);
}
});
}
});
});
URL from front end :
// order is a string
here is the Log schema :
const mongoose = require("mongoose");
const LogSchema = new mongoose.Schema(
{
// _id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
description: { type: String, required: true },
price: { type: Number, required: true },
quantity: { type: Number, required: true },
modified_quantity: { type: Number, required: true },
supplier: String,
taxable: Boolean,
itemDest: String
},
{ timestamps: true }
);
// Create model from the schema
const Log = mongoose.model("Log", LogSchema);
// Export model
module.exports = Log;
and here is the inventory schema
const mongoose = require("mongoose");
//create Schema
const InventorySchema = new mongoose.Schema(
{
// _id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
description: { type: String, required: true },
price: { type: Number, required: true },
quantity: { type: Number, required: true },
supplier: String,
taxable: Boolean
},
{ timestamps: true }
);
// Create model from the schema
const Inventory = mongoose.model("Inventory", InventorySchema);
// Export model
module.exports = Inventory;
My issue is with this line "itemDest: order" in the query, I intend to save the value of "order" extracted from "req.params.order" into "itemDest" but it doesn't save.
i've created an api which works fine and i have added a user through an post requet however when i try to add another user with a different id i get this error:
SUCCESS: {
code = 11000;
errmsg = "E11000 duplicate key error index: matchmaking.users.$id_1 dup key: { : null }";
index = 0;
op = {
"__v" = 0;
"_id" = 352523;
birthday = "1998-10-09T23:00:00.000Z";
country = denmark;
name = Frederik;
};
}
UserSchema
var userSchema = new Schema({
_id: {
type: SchemaTypes.Long,
required: true,
unique: true
},
accessToken: String,
email: String,
name: String,
birthday: Date,
country: String,
created_at: Date,
updated_at: Date
});
I don't understand why I got this error below after adding the first entry:
E11000 duplicate key error index: mydb.datasets.$id_1 dup key: { : null }
I don't have any null value in my first entry:
{
"index" : "9IPZMW7IL",
"name" : "Tweets",
"owner_name" : "xxx",
"read_key" : "fb6f9125f4ca15c33fea89416c3351d1",
"write_key" : "d8a6c7e5fc73b5a91aa7a533565ed1f1",
"data" : {
"var1" : {
"name" : "particles"
}
},
"_id" : ObjectId("57729dc20cb70952424cdbb4"),
"created_at" : ISODate("2016-06-28T15:54:42.576Z"),
"entries_number" : 0,
"public" : true,
"__v" : 0
}
Below is my code:
// CRUD API:
// POST/ Create new dataset request
router.post("/", helper.authenticate, function(req, res) {
// Used to set the dataset owner
var sessionUser = req.session.user.name;
// Get values from the post request
var name = req.body.name;
var isPublic = req.body.public != undefined ? true:false;
// Delete the values from the request body so that we only keep information about the variables
delete req.body.name;
delete req.body.public;
// This is so that we can loop through the object in reverse order
// We do that so that the fields are saved in the right order on the db
// (this way it will appear in the right order on the 'edit' view)
var propertiesList = [];
for (var property in req.body) {
if (req.body.hasOwnProperty(property)) {
propertiesList.push(property);
}
}
propertiesList.reverse();
var variablesFields = {};
for (var i in propertiesList) {
console.log(propertiesList[i])
variablesFields[propertiesList[i]] = {name:req.body[propertiesList[i]],
values: Array};
}
// Create dataset
Dataset.create({
index: helper.uniqueIndex(),
name: name,
owner_name: sessionUser,
read_key: hat(),
write_key: hat(),
public: isPublic,
data: variablesFields
}, function(err, dataset) {
if (err) {
console.log("Error creating the dataset: " + err);
req.session.error = "A problem occured when creating the dataset. Please try again.";
} else {
console.log("New dataset created with id: " + dataset._id);
req.session.success = "Dataset " + name + " created successfully.";
}
res.redirect("/index");
});
});
Error:
Error creating the dataset:
WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key
error index: mydb.datasets.$id_1 dup key: { : null
}","op":{"index":"2IPZMWHGI","name":"PM
2","owner_name":"xxx","read_key":"fc31c152aa86070252c70c0304e4ca5c","write_key":"238110753c8762ce4a547fa02100a299","data":{"var1":{"name":"particles"}},"_id":"57729dcf0cb70952424cdbb5","created_at":"2016-06-28T15:54:55.459Z","entries_number":0,"public":true,"__v":0}})
Model:
var datasetSchema = new mongoose.Schema({
index: {type: String, required: true, index: {unique: true}},
name: {type: String, required: true},
owner_name: {type: String, required: true},
read_key: {type: String},
write_key: {type: String},
public: {type: Boolean, default: false},
data: {type: Object},
entries_number: {type: Number, default: 0},
created_at: {type: Date, default: Date.now},
last_entry_at: {type: Date}
});
Any idea why and how I can fix this?
I solved it by removing a "id" key that I initially declared as
id: { type: String, unique: true, required: true},
I removed this line and deleted the initial collection and that solved the issue.