Mongodb find where exists in other collection - mongodb

I have a user collection and a document one, where entries have a owner field that is a reference to the user ObjectId(_id).
I'm amazed I can't find all users that have at least a document...
I tried:
db.getCollection('user').find({_id: {
$in: db.getCollection('document').find({}).map(function(f) {return f.owner}).distinct()
}});
but it won't work and anyway it really feels like not the correct way to do this since all documents need to be loaded in memory.
I tried to use http://www.querymongo.com but it really did not help.
Thanks

Related

Mongodb use array as unique key

I want to make sure I only have one instance a documents that is an agreement between to entities... I may have modeled this wrong, so Im looking for suggestions if there is a better way to do this.
but in essence, the document looks like this.
{
_id:objectid,
entity_Ids: [objectid]
}
I was curious what's the best way to make sure the array is unique so id I do
db.agreements.insert(entity_Ids:[1,2]) and there is already a 1,2 I'll get a conflict.
I could have done it like this :
{
_id:objectid,
entity1: objectid,
entity2: objectid
}
but then I'd always have to do a check of where entit1=1 or entity2=1 or entity1=2 or entity2=2
you can create unique index on any key in the document
so in your case try
db.myCollection.createIndex({entity_Ids: 1},{unique: true});

How do I make a mongo query for something that is not in a subdocument array of heterodox size?

I have a mongodb collection full of 65k+ documents, each one with a properties named site_histories. The value of it is an array that might be empty, or might not be. If it is not empty, it will have one or more objects similar to this:
"site_histories" : "[{\"site_id\":\"129373\",\"accepted\":\"1\",\"rejected\":\"0\",\"pending\":\"0\",\"user_id\":\"12743\"}]"
I need to make a query that will look for every instance in the collection of a document that does not have a given user_id.
I'm pretty new to Mongo, so I was trying to make a query that would find every instance that does have the given user_id, which I was then planning on adding a "$ne" to, but even that didn't work. This is the query I was using that didn't work:
db.test.find({site_histories: { $elemMatch: {user_id: '12743\' }}})
So can anyone tell me why this query didn't work? And can anyone help me format a query that will do what I need the final query to do?
If your site_histories really is an array, it should be as simple as doing:
db.test.find({"site_histories.user_id": "12743"})
That looks in all the elements of the array.
However, I'm a bit scared of all those backslashes. If site_histories is a string, that won't work. It would mean that the schema is poorly designed, you'd maybe try with $regex

how to join a collection and sort it, while limiting results in MongoDB

lets say I have 2 collections wherein each document may look like this:
Collection 1:
target:
_id,
comments:
[
{ _id,
message,
full_name
},
...
]
Collection 2:
user:
_id,
full_name,
username
I am paging through comments via $slice, let's say I take the first 25 entries.
From these entries I need the according usernames, which I receive from the second collection. What I want is to get the comments sorted by their reference username. The problem is I can't add the username to the comments because they may change often and if so, I would need to update all target documents, where the old username was in.
I can only imagine one way to solve this. Read out the entire full_names and query them in the user collection. The result would be sortable but it is not paged and so it takes a lot of resources to do that with large documents.
Is there anything I am missing with this problem?
Thanks in advance
If comments are an embedded array, you will have to do work on the client side to sort the comments array unless you store it in sorted order. Your application requirements for username force you to either read out all of the usernames of the users who commented to do the sort, or to store the username in the comments and have (much) more difficult and expensive updates.
Sorting and pagination don't work unless you can return the documents in sorted order. You should consider a different schema where comments form a separate collection so that you can return them in sorted order and paginate them. Store the username in each comment to facilitate the sort on the MongoDB side. Depending on your application's usage pattern this might work better for you.
It also seems strange to sort on usernames and expect/allow usernames to change frequently. If you could drop these requirements it'd make your life easier :D

Finding embedded document without _id in MongoDB

I am trying to find an answer on an issue that is keeping me busy for days. I would like to find and or update an embedded document in a MongDB collection (using mongoose). It is possible I know. You can do something like this:
User.findOne({_id: id}, function(err, user) {
var embeddedDoc = user.embeddedDocs.id('embeddedDocId');
});
This works indeed. However, only if you have the specific docId which I do not know (the embedded doc can be an item(x) of an array).
My question is, is this possible anyway? Or does mongodb not let us find an embedded doc without supplying an Id?
Regards, Douwe.
Yes, you can.
Use the dot notation to specify the property of the embedded document you are looking for.
Let's say you have a collection for persons with docs like this:
{
_id:123,
name:"john",
address: {
Street:"any",
zip:1234
}
}
Then you can find a person querying by an address field like this:
db.persons.find("address.zip":1234, ...)
Just consider that it won't give you only the embedded doc, but the entire document (person in this case)

Does Mongo make a mistake like this?

Say I have a User Document, filled with arrays of ObjectIds.
They are references to documents in another collection.
I want to load all things from a particular user's array. So I do:
find({ _id: $in : someArrayOfObjectIds})
It's possible that certain references reference something that has been deleted.
So the resulting array of the above "find" call can be smaller then the someArrayOfObjectIds.
So for all the ObjectIds not found can I now safely assume that that document does not exist anymore, or can my query just fail to find a document (does mongo make a mistake).
Yes, you can safely assume that missing documents do not exist. By the way, your query is invalid. Should be this:
find({ _id: {$in : someArrayOfObjectIds}})
or can my query just fail to find a document
If it was possible, no one would use it. Pen and paper approach is a safer alternative that DB that makes such mistakes :)