I have an Mongoose database object which contains a collection that holds "user signatures", where the signature is must to be unique, i.e no duplicates of signatures can appear in the collection.
I have therefor put the unique: true flag on the field signature in the schema object, but this still don't prevents it from having duplicate, but it do work on title
query
You can modify your filter and include $elemMatch condition to make sure that the element you're trying to insert into an array isn't already there:
let filter = { _id: id, signatures: { $not: { signature: signature } } };
Mongo Playground example
So if you're trying to insert a new signature the document will be found by _id otherwise the filter part will return no matching document and your update won't be applied.
Related
I have a collection named ABC and when I am running the following query in node(12 OR 14) it also returns documents which "r_o" doesn't exist in it.
const aRef = firebase.collection('ABC');
let querysnap = await aRef.where('r_o', '!=', false).limit(50).get();
I have around 900K documents in the 'ABC' collection. Most don't have the field at all, some have it with a value of true(boolean), So in most of the documents "r_o" doesn't exist at all and the query should not return those documents but it does.
So looks like the query ignores ('r_o', '!=', false) and simply returns all 900K documents in the collection.
To clarify, I want to get all the documents only and IF only the field value is true, I don't want to have the documents if the field value is false OR field doesn't exist.
Per firebase documentation and in their example:
citiesRef.where("capital", "!=", false);
This query does not return
city documents where the capital field does not exist. Not-equal (!=)
and not-in queries exclude documents where the given field does not
exist.
PS: I am using package firebase-admin v9.0.0
I update 100's of documents every second by pushing new data to an array in its document. To get the document of which I am going to add data to, I use the mongoose .find().limit(1) function, and return the whole document. It works fine.
To help with some memory and cpu issues I have, I was wondering how I could get find() to only return the id of the document so I can use that to $push or $set new data.
Thanks.
You want to use Projection to tell your query exactly what you want off of your objects.
_id is always included unless you tell it not to.
readings = await collection
.find({
name: "Some name you want"
})
.project({
_id: 1 // By default
})
.toArray();
You could use the distinct method in order to get an array of _id for your query.
Follow this question
As mentioned by #alexmac, this works for me:
collection.find({}, '_id')
I am creating a meteor app that gets sensor data from remote devices via POST, and need to query on a value in a mongo collection, returning the document _id.
The document is similar to this:
[{
_id: 0,
device: {
mac: 'XXX', // always unique
sd1: nnn,
sd2: nnn,
...
}
}]
I need to be able to do a findOne() on XXX, determine if it exists, and get the _id of the corresponding doc.
I've tried variations of collection.findOne({}, {mac: 1}), but that only returns the first document that contains the key 'mac'. As every doc will contain that key (but the value will always be unique), this is pretty useless as it always returns the first document in the collection. I need to query on the value of the key.
There must be a very obvious solution, but it has eluded me thus far.
If you had a collection as such.
user {
name: String,
email: String,
information: String
}
You would do something like so to get a list of all John's that have information of doctor.
db.user.find({name: "John", information: "doctor" });
Now this makes the code redundant when having variable inputs. Such as having permutations of fields to filter. I'm trying to make my query generic, such as this bad broken example.
Therefore I might want to be able to explicitly state fields that can match any value. The following examples should return the same documents in theory.
Example:
Un-Explicit (normal) db.user.find({});
Explicit (weird) db.user.find({name: {$ANY}});
Or make it even more complex.
db.user.find({name: {$ANY}, information: "doctor"});
This would not work, but the intention is to get all the documents that are doctors but have ANY sort of value on the name field, not just for John's. Maybe even something more complex like so.
db.user.find({name: function(){
if(req.query.name)//check if empty
{ return req.query.name; }
else { return $ANY; }
}, information: "doctor"});
This would then be generic enough to use a single query instance for dynamic request behavior.As far as I know there isn't anything like this implemented.
Thank you in advance.
To get all the documents that are doctors but have ANY sort of value on the name field, you need the $exists and $ne operators in your query, since the $exists operator matches the documents that contain the field, including documents where the field value is null and the $ne operator selects the documents where the name field is not null:
db.user.find({"name": { "$exists": true, "$ne": null }, "information": "doctor"});
If you want to find all the documents with information = 'doctor', why not just query without the name?
db.user.find({information: "doctor" });
I have to run a dynamic query on collections in such a way that user enters collection name, field name and field value and I have to first of all check whether the field name supplied by user exists in the collection , if not then I have to run another query as shown in the example below.
For example:
If user enters collection name user, field name is type and field value is article. And based on parameters I have to query as follows:
1) If type field exists in collection user, my query will be:
query = user.find({type:'article'})
2) If type is not field of collection user, my query will be:
query = user.find()
I tried $exists but it is not working for me. And how can I use $exists before querying? because my collection, field name and field value all are dynamic.
And How can I check that type field exists or not in user collection?
I already tried the solution with $exists, but I want to know if there is another way to do it.
As #Philipp mentioned in his comment, you are bound to bump into performance issues as you would need to perform a full-collection scan without the use of indexes. Using $exists, the query will return fields also equal to null. Suppose you want to first find all records who have the field type set and isn't null, then use a combination of the $ne operator. The closest you could try is get the count of that field and then determine the query object based on the field count:
var type_count = db.user.find({"type": {$exists: true, $ne: null}}).count();
var query = (type_count > 0) ? {"type": "article"} : {};
db.user.find(query);