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
Related
I have a database with values like this:
I also have a post method with the goal of updating the startBudget when a form is submitted.
All I want to do is update the startBudget number in the database. I tried doing that by using the updateOne function but it didn't change anything in the database. What am I doing wrong?
Figured it out. It turns out I needed to use save instead of updateOne. All good.
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/
I'm using Node.js v8.12.0, MongoDB v4.0.4 & Mongoose v5.3.1.
I want to update an embedded array subdocument without changing its position in array index. And without loading the entire array in memory because that array might get very big in future.
I spent a lot of time searching how to achieve this but without luck.
I've tried to use Mongo's $elemMatch to load the document with only single relevant array subdocument, but that wouldn't let me save the subdocument in any practical way.
I've also looked into Mongoose array set method, but for this you need to have subdocument array index, so this mean I need to load the entire array into a memory and then find subdocument index.
Is there any way to achieve what I want? Ideally via Mongoose abstractions, so all Mongoose middleware like validations, plugins, etc. kick in.
Actually found the answer right after submitting this question.
For anyone looking it up in future, you can use special $ operator in combination with $set. (I'm not sure if that makes use of mongoose middlewares, though. I need to do some testing around that).
Here's some code example, which queries some subdocument using _id, and then updates that subdocument property someProperty using $set which uses special operator $ that matches queried subdocument array position/index.
var doc = await SomeModel.findOneAndUpdate(
{'someArray._id': 'xxx'},
{'$set': {'someArray.$.someProperty': 'someValue'}},
);
It's still not 100% what I'm looking for, though. Because the returned doc still has full array of subdocuments (I assume that's still loaded into memory?), and it seems that I can't use $elemMatch in combination with $set.
In my case, further down the road, I'll probably redesign the database schema so these embedded subdocuments will be their own documents with just ObjectID reference to "parent" document.
Some resources:
https://docs.mongodb.com/manual/reference/operator/update/positional/
https://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate
I am doing a query with solr where I need to find documents without a given field say 'name' and I am trying following part;
$q=+status:active -name:["" TO *]'
But it sends both all the documents with and without that field.
Can anyone help me figure this out?
the field name is a normal String type and is indexed.
I am using nodejs. Can anyone help me with this
According to docs:
-field:[* TO *] finds all documents without a value for field
Update
I tried it but it sends even the ones with the field non empty
Then my wild quess is that you are using search query q instead of using filter query fq. Since you are using multiple statements in query I assume that q does some extra magic to get the most relevant documents for you, which can lead to returning some non-wanted results.
If you want to get the strict set of results you should use filter query fq instead, see docs.
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 :)