Is $nin db.collection.find() valid - mongodb

I'm using mongodb shell and trying to do a collection query within another collection query. Is it valid to do this:
db.watchers.find( { login: { "$nin": [db.results.find()] } } )
I'm trying to see if the login id from db.results.find() is in db.watchers.find(). Can mongodb even do that?

Mongodb does not let you do subqueries. This is invalid.

Related

How to extract the creation date of _id and add it as a new field using the aggregation framework?

I have been trying for a while to extract the insertion date of a mongodb document and add it as a new field to the same document.
I'm trying to do it using the mongo and mongo shell aggregation framework without getting good results.
Here is my query
db.getCollection('my_collection').aggregate(
[
{
$match: {
MY QUERY CRITERIA
}
},
{
$addFields: { "insertTime": "$_id.getTimestamp()" }
}
]
)
I am trying to extracr insertion time from _id using the function getTimestamp() but for sure there is somtehing about aggregation framework syntax that I am missing because I can not do what I am trying to do in my query.
This works perfect:
ObjectId("5c34f746ccb26800019edd53").getTimestamp()
ISODate("2019-01-08T19:17:26Z")
But this does not work at all:
"$_id.getTimestamp()"
What I am missing?
Thanks in advance

How do I formulate a query using a sub Object as a field?

How do I formulate a query that uses a sub Object field to return a cursor?
My Object structure looks like this:
_id: "4ncBGvppEs92e4tcZ"
expiryDate: "2017-05-27T21:45:57+03:00"
viewStatisticsArray: Array[1]
0: Object
nrOfViews:155
statesDate:"Fri Mar 24 2017 12:46:21 GMT+0300 (EAT)"
viewedBy: "udEnfEmy5DSBvDsSy"
Please note that the viewedBy (also what I am refering to as a Sub Object) value carries my user id generated from Meteor.user()._id;
var MyUserId = Meteor.user()._id;
I have unsucessfully tried:
Db.find({},{viewedBy: { $in: MyUserId } } ).fetch();
Also unsucessfully tried:
Db.find({},{viewStatisticsArray.viewedBy: { $in: MyUserId } } ).fetch();
Please help! How do I forumlate the query above correctly?
The expected results should yeild in a cursor full of Objects that only have viewedBy: "udEnfEmy5DSBvDsSy".
In mongodb (and therefore Meteor), the correct way to execute this type of query is to use the $elemMatch operator. As an example, your query would look like this.
Db.find({
viewStatisticsArray: {
$elemMatch: {
viewedBy: MyUserId
}
}
});
Since, however, you are only needing to specify a single query predicate, you can alternatively use the below query as well.
Db.find({
'viewStatisticsArray.viewedBy': MyUserId
});
One word of caution, I don't know if minimongo (the mongodb like db that runs on the client in a meteor app) support the $elemMatch operator. If that is the case, and if you are executing this query on the client, then you must use the 2nd option above.

Mongo DB find() query error

I am new to MongoDB. I have a collection called person. I'm trying to get all the records without an _id field with this query:
db.person.find({}{_id:0})
but the error is
syntax error: unexpected {
but if i write
db.person.find()
it works perfectly.
Consider following documents inserted in person collection as
db.person.insert({"name":"abc"})
db.person.insert({"name":"xyz"}
If you want to find exact matching then use query as
db.person.find({"name":"abc"})
this return only matched name documents
If you want all names without _id then use projeciton id query as
db.person.find({},{"_id":0})
which return
{ "name" : "abc" }
{ "name" : "xyz" }
According to Mongodb manual you have little wrong syntax, you forgot to give comma after {}
Try this :
db.person.find({}, { _id: 0 } )

How to translate this Mongodb clause into mongoengine clause?

db.students.find( { grades: { $elemMatch: {
mean: { $gt: 70 },
grade: { $gt:90 }
} } },
{ "grades.$": 1 } )
this is on official mongodb document, I have troubling translate them into mongoengine clause.
Can anybody translate this mongodb clause into mongoengine clause?
Because I have a embedded document in an array, I need to filter something inside the embedded document, I found this on the document, but don't know the exactly way to translate it. thank you!
I got the answer, but I do it another way around, I implement elasticsearch into my mongodb to be the search engine, so that's not the problem

Cannot remove on mongodb using mongoose?

Hi im trying to simply remove a document from a collection using mongoose but for some strange reason I cannot get it to work.
Here is the code:
function deleteUserevent()
{console.log('in delete User Event');
models.Userevent.remove({ _id: "5214f4050acb53fe31000004"}, function(err) {
if (!err){
console.log('deleted user event!');
}
else {
console.log('error');
}
});
}
Can anyone help me out on my syntax? I know the _id is stored as new ObjectId("5214f4050acb53fe31000004") but I have tried this with no joy?
Thanks.
In MongoDB, the "_id" field of documents is of type ObjectId, as you mentioned. This is not equal to a String, so running the query
db.userevent.remove({ _id: "5214f4050acb53fe31000004"});
will not match anything, and will not remove anything. Instead, you must search for a document where the _id field is an ObjectId with that value:
db.userevents.remove({ _id: ObjectId("5214f4050acb53fe31000004")});
In mongoose, you can use the findByIdAndRemove command to remove a document with a specific _id. This command takes either an ObjectId or a String as an argument, so
query = Userevent.findByIdAndRemove("5214f4050acb53fe31000004");
should work just fine.
Just add exec() after query.
It should work like this:
await models.Userevent.findByIdAndDelete("5214f4050acb53fe31000004").exec()