how to get results from mongoDB where _id is string objectId - mongodb

in my mongo database _id is string from ObjectId like this:
when I make this query I get results but with wrong sorting
db.collection.find({ _id : {$gt:"57c03e6288579757b5172d51"} });
how to fix that ?

You cannot query by inserting the objectID string with $gt operator.
You will have to create an objectID.
var objID = ObjectId("57c03e6288579757b5172d51");
db.collection.find({ _id : {$gt: objID} });
Documentation

Related

Mongoose $nin with array of ObjectId

I am facing a problem with mongoose. I can't do a query filter with $nin or $ne on an array of ObjectId.
Like:
query: {
objectIds: { $nin: [ objectId1, objectId2 ] }
}
I tried to send an ObjectId.toString(), an ObjectId, but nothing works.
However, if I replace the ObjectId array by a string array or put ObjectId in it, it works.
Any idea why it doesn't work with ObjectId?

MongoDB: Search by id and other properties

I have Mongo document
{
"_id" : ObjectId("5e4c4c0935f4115dfc5540c2"),
"code" : "ABC"
}
and as per other SO threads looks like doing regex on Id is not possible as it has hexString. So I am querying with ObjectId
#Query("{'code': {$regex : ?0, $options: 'i'}, '_id': ?1}")
Page<Pack> findByCodeAndId(String code, ObjectId objectId, Pageable pageable);
now as the query paramets or optional, I have check if the passed id is valid before converting it to ObjectId
ObjectId objectId = null;
if (ObjectId.isValid(id)) {
objectId = new ObjectId(id);
}
now for the request {code: null, id: null} I want to bring first 10 documents as these are optional but the query is not working when it is passed null as ObjectId
Also tried ObjectId objectId = new ObjectId() but it is generating a random haxString and giving empty records when id is null

Mongo Upsert with values of find $in

I have to run mongo updates by querying that the user id is in an array. Is it possible to upsert any values not in the array?
Eg:
db.collection.update({
userId:{
$in:['1','2', '3']
}
},
{$set: {score:30}},
{upsert:true})
If I run this query I get one new doc with _id and score. What I'd like to do is have a new doc for each userId not present in the userId array:
[{userId:1, score:30, _id:...}, {userId:2, score: 30, _id: ...}, ...]
Is this possible in mongo?
No, this cannot be done. The documentation states:
If upsert is true and no document matches the query criteria, update() inserts a single document.

mongodb $where with another indexed field

Mongodb doc says $where does a collection scan, which is bad, but if I add an indexed field in the query, will that take advantage of indexed speed?
db.users.find({
_id: someMongodbId,
$where: function() { return this.name == 'joe' } // ensure this doc has name "joe"
});
In the example above, there's only 1 query because of _id field, right?

MongoDB update collection's data

I try to update an MongoDB data by using this code:
db.medicines.update({"_id":"586a048e34e5c12614a7424a"}, {$set: {amount:'3'}})
but unfortantly the query does not recognize the selector "_id":"586a048e34e5c12614a7424a", even if its exists.
Its succsed when I change the key to another like: name,rate and etc..
there is a special way to use update with _id parameter?
Thanks a head.
_id will be the unique ObjectId that mongodb generates for every document before inserting it. The query dint work because _id is an ObjectId and "586a048e34e5c12614a7424a" is a String. You need to wrap _id with ObjectId().
If you're using mongodb query
db.medicines.update({
"_id": ObjectId("586a048e34e5c12614a7424a")
}, {
$set: {
amount: '3'
}
});
If you are using mongoose. You can use findByIdAndUpdate
db.medicines.findByIdAndUpdate({
"_id": "586a048e34e5c12614a7424a"
}, {
$set: {
amount: '3'
}
});