How to remove the deprecation warning in nodejs - node.js

I'am using mongoose-ttl node package to clear the collections. But I'am getting this warning.
(node:4768) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
I'am not using any methods like remove() etc.But I can't understand from where this warning is thrown.
Please suggest me any method to remove this warning.
TTL Code goes here:
var mongoose = require("mongoose");
var ttl = require('mongoose-ttl');
var OTPStore = new mongoose.Schema({
OTPNo: {
type: Number,
require: true
}
});
OTPStore.plugin(ttl, { ttl: '30m', interval: '30m'});
module.exports = mongoose.model("OTPStore", OTPStore);
Thank you.

Looks like you wanna delete something from db.
const store=await OTPStore.deleteOne({_id:id}) //this will find the first one and delete
const store=await OTPStore.deleteMany({ _id:id })
//this will delete all. This used for properties that some of the instances have others not. For example if you had "isExpensive" property in your schema you could pass {isExpensive:true} and mongose would find all the instances that meet this criteria and remove them
const store=await OTPStore.findByIdAndRemove(id)
// if you notice in above examples I passed an object. But here you are already notifying mongooose that you are querying by id, so just put an id as argument
NOTE that this operations are async operations so in order to use await, make sure wrapper function should start with async function

Related

is changed function in sequelize like isModified in mongoose?

mongoose isModified functionIn a hook I want to confirm whether a password has changed before executing encryption process. Mongoose has a function "isModified" and I believe Sequelize's "changed" function servers the same purpose.
I cannot get the "changed" function to work. I am looking for an example of how it is used.
There is an example of how changed() works in the official documentation
const mdl = await MyModel.findOne();
mdl.myJsonField.a = 1;
console.log(mdl.changed()) => false
await mdl.save(); // this will not save anything
mdl.changed('myJsonField', true);
console.log(mdl.changed()) => ['myJsonField']
await mdl.save(); // will save
Keep in mind that changes are detected for the top-level fields and only for changes that were made since the last save call.

Mongoose and bulk update

First time working with MongoDB and Mongoose.
I have written an edit function which do something with the input params and then call an update on the model:
edit: function (input) {
var query = ...
var document = ...
return Model.update(query, document, {multi: true});
}
The function return a Promise with the number of affected documents.
I know that the Mongoose update function
updates documents in the database without returning them
so I was wondering if there is a way to somehow:
run my edit function
if everything goes well, run a find on the model in order to retrieve the updated documents, and return the find's Promise.
Any help would be appreciated.

Why isn't my Q promise working?

I'm new to promises and Q, and I'm trying to convert a route that uses query in node-mysql. Here are excerpts from my code:
var Q = require('q');
// connection defined elsewhere
router.get('/', function(req, res) {
var query = Q.denodeify(connection.query);
var promise = query("-- query ommitted --", [req.user.id]);
promise.then(console.log, console.error);
});
I'm trying to convert this from an existing set up that doesn't use promises, so I know the connection is set up properly and the query is valid. Whenever I try to request this route, I get the same message in stderr:
[TypeError: Cannot read property 'connectionConfig' of undefined]
I don't know where it's coming from so I'm not sure how to find the full stacktrace. I also tried an alternate version of this code where I had my own function instead of console.log and console.error, but this function was never called and the same error appeared.
Updated answer:
You're probably losing the lexical scope to connection when you denodeify the query method.
Looking at the Q documentation it says this can be an issue:
If you are working with methods, instead of simple functions, you can easily run in to the usual problems where passing a method to another function—like Q.nfcall—"un-binds" the method from its owner.
To fix this try using Q.nbind instead:
var query = Q.nbind(connection.query, connection);
var promise = query("-- query ommitted --", [req.user.id]);
promise.then(console.log, console.error);
Original answer:
Looking at the node-mysql source, the only place connectionConfig is accessed is in Pool.js. So my guess would be that you've got an issue with how the pool is being configured.

Native driver find from Mongoose model not returning Cursor

I'm trying to execute a native MongoDB find query via the collection property of a Mongoose Model. I'm not supplying a callback so I expect the find to return a Cursor object, but it returns undefined instead. According to the Mongoose docs, the driver being used is accessible via YourModel.collection and if I switch to purely using the native driver code find does return a Cursor so I can't figure out what's going on.
Here's a code snippet that reproduces the problem:
var db = mongoose.connect('localhost', 'test');
var userSchema = new Schema({
username: String,
emailAddress: String
});
var User = mongoose.model('user', userSchema);
var cursor = User.collection.find({});
// cursor will be set to undefined
I've tried to step into the code with node-inspector, but it's not letting me. Any idea what I'm doing wrong?
The native driver methods are all proxied to run on the nextTick so return values from the driver are not returned.
Instead, you can pass a callback and the 2nd arg returned is the cursor.
User.collection.find({}, function (err, cursor) {
//
});
Curious why you need to bypass mongoose?

Having problems exporting model functions (Express and Mongoose)

I have been looking at code (https://github.com/cmarin/MongoDB-Node-Express-Blog) to learn NodeJS, Express, Mongoose, and I am having trouble importing a 'Poll' function from my 'models.js' file, particularly the 'save' function.
I am getting the following error:
500 TypeError: Object function (){} has no method 'save'
It occurs on line 54 of my app.js. I am unable to save a new Poll because it cannot find the function:
https://github.com/kelper/Poll/blob/master/app.js
Here is my models file, and the save function is on line 62:
https://github.com/kelper/Poll/blob/master/models.js
One other quick question. How can I exclude files from being committed? I keep committing swap files and such to my repo.
If you see anything else wrong with my code, please tell me. I know one person mentioned that my naming conventions are confusing. How should I be naming my variables?
PollModel is a function constructor, you want to create an object.
var PollModel = require('./models').PollModel; is wrong
var pollModel = new (require('./models').PollModel); is right.
Looks like you've got a proxy object built up using prototype. In this case you're going to have to 'new up' an instance to use it as Raynos mentioned.
I think what you're expecting is what an object literal provides, rather than a prototypical class. Something like:
module.exports.PollModel = {
findPolls : function (callback) { ... },
findById : function (id, callback) { ... },
updateById : function (id, body, callback) { ... }
}
I'd personally use the mongoose schema directly.
Mongoose uses the Schema object to do queries for that particular model, but if you actually want to create and save new objects of that schema type, you want to new up a new object.
// Assume you've exposed the mongoose Poll schema directly
var Poll = require('./models').Poll;
// Create a new instance
var instance = new Poll();
// Valid
instance.save();
// Not valid
instance.find(function(err, docs){});
// Valid
Poll.find(function(err, docs){});
// Not valid
Poll.save();

Resources