Multiple Inputs in ne mongoose method - node.js

I have users collection and I would like to get all users where status doesn't equal 'deleted' or 'pending'
I tried this
users.find().where('status').ne('deleted').where('status').ne('pending')
but it doesn't work.
I also tried this
users.find().where('status').ne(['deleted','pending'])
Also doesn't work.
How would I accomplish it?
I know it can be accomplished using a nested query string, but I want a way with the help of these query methods.

If you want to do multiple $ne then do
users.find({status: {$nin : ["deleted", "pending"]}})
https://docs.mongodb.com/manual/reference/operator/query/nin/

Related

How to compare two multiple fileds in document in mongodb

i have collection called "devices". this devices collection have two fields called as "devices" and "totalDeviceNeeds". totalDeviceNeeds have integer value. devices field contain array. I want to get all documents that totalDeviceNeeds > devices array size. How can i do that. I try lot of ways and didnt get correct. I try in nodejs.
Thank you all.
i just wrote query code or whatever called. here is this
db.collection.find({ $expr:{ $lt:[{$size:"$devices"}, "$totalDeviceNeeds"] } }).toArray();
in this $lt meaning less than operation
i used this post to do this.
https://www.mongodb.com/community/forums/t/is-there-a-way-to-query-array-fields-with-size-greater-than-some-specified-value/54597/2

Empty a MongoDB (via Azure Cosmos Db) shared collection with deletemany

I am currently working on a NodeJS project handling datas with MongoDB via microsoft AzureCosmosDB.
For the good use of the project, I have a shared collection (with the _id as shardkey) that I would like to empty regularly, I know that this is done using the "deleteMany" instruction with an empty object as parameter.
So I tried and I am currently facing a recurrent error :
query in command must target a single shard key
I understand the logic behind this error, but I don't know where to start to find a solution, and didn't find any help in the mongo documentation.
I've read about using hashed shardkeys and how this make the use of shardkeys more "flexible", but I would like to know if there is an easyer solution, maybe something i've missed that would allow me to empty the collection without giving all the item ids one by one :)
Any idea ?
Thank you very much !
SO
It appears that this is not currently possible, and that the Azure CosmosDb team is working on it, with a tentative of release date in the firsts months of this year (2019).
https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/34813063-cosmosdb-mongo-api-delete-many-with-partition-ke
Thank you for the help and sorry for bothering
You should be able to make a query or delete command by matching any document in the collection that has _id field:
db.collection.deleteMany({ _id: { $exists: true }})

How to change a certain fields name in all my documents with mongojs?

I have a collection, in this collection all my records have, for example, a field named "car".
I get a new name for this field from my form and i would like to go through all my records and change only the fields name, without changing the value.
I have tried nothing, i can't think any method to do this.
This i what i have:
{car:"dodge"}, {car:"ford"}
This i what i would like to get:
{vehicle:"dodge"}, {vehicle:"ford"}
What is the easiest method for this?
The MongoDB docs contain a list of all available update operators. In this case you'd want to use $rename.
In mongojs:
db.coll.update({}, {$rename: {'car': 'vehicle'}}, {multi: true}, callback);

Get documents with field that isn't a particular string

I want to get the documents from MongoDB that don't have the field "name":"John" (just an example).
How can I do this?
For getting the documents that have the field "name":"John" I use:
db.col.find({"name":"John"})
I've already tried ...find(!{"name":"John"}).
db.col.find({"name":{$ne: "John"}})
should work, here are docs: Mongo, Advanced Queries.
Just use $ne selector like this:
db.col.find({"name":{$ne:"John"}})
http://docs.mongodb.org/manual/reference/operator/ne/#_S_ne

Mongoose renaming _id to id

I'm creating an API that is using data from Mongoose. The front end (Backbone) expects id, instead of _id. I cannot seem to find an easy solution to such a simple problem. Is anyone aware of a way to rename _id to id. I want this to be default behavior across every schema.
Did you think of setting model.idAttribute to _id on the front end (Backbone). This would allow Backbone to 'transparently map that key to id'.
http://backbonejs.org/#Model-idAttribute
You can set up a schema method getItem which returns the desired fields and id = _id, if you really need that :)

Resources