Check if document exists in different collection MongoDB Aggregation [duplicate] - mongodb

This question already has an answer here:
How to Model a "likes" voting system with MongoDB
(1 answer)
Closed 6 years ago.
I insert a new document in Votes collection when a user votes on a poll.
{
_id: ObjectId(XXX),
card: 11,
user: 22
}
Now when a user requests for all the polls I want to return a Voted: 1 field if the users have already voted on the poll i.e. a document is already present in the Votes collection.
Can anyone tell me if there's a way to access documents from another collection in aggregation command.

With mongoDB it's not possible to access multiple documents within a query. You should change your data model and add an array or use embedded documents.
I don't know much about your use case so please take this just as an example and not as a final solution.
The following model contains an array for all voted polls of an user. Therefore you can check if the array contains the poll and return 1 if its true.
{
_id: ObjectId(XXX),
user: 22,
cards: [1, 3, 5]
}
See https://docs.mongodb.org/manual/core/data-modeling-introduction/ for more details about data modelling in mongoDB.

Related

Firebase where query (not include) [duplicate]

This question already has answers here:
Firestore - Possible to query by array-not-contains?
(4 answers)
Firestore get documents where value not in array?
(4 answers)
Closed 1 year ago.
I have a firestore collection with many document and all these documents include an array field (called "notFor") with a list of uid. In some cases the array contains only 1 value in some other cases it contains more than 1 value. Example
docA
notFor: "userB", "userC"
docB
notFor: "userB"
docC
notFor: "userA", "userC"
Is there any suggestion on how I could find a document in that collection that does not include "userB"? I basically would like to find docB by excluding all the docs where userC is present
I am writing in flutter however this is not so relevant for this case, I am just interested to understand if I can do something like this with the firestore operators.
thanks!

How to store Invoice Items in MongoDB? [duplicate]

This question already has answers here:
MongoDB relationships: embed or reference?
(10 answers)
Mongoose populate vs object nesting
(1 answer)
Closed 3 years ago.
I've to store line items in the database.
Currently, I'm creating a batch of them and storing it as a single document.
date: null,
items: [
{ name: 'Bla', cost: 5, ref: 'user id' },
{ name: 'Bla', cost: 5, ref: 'user id' },
]
But I'm having a gut feeling that it will be more costly (performance and processing) because it is an array of object. ($unwind, $project).
Will it be good if I store each line item as a document instead?
The reason why I've not done so far is we may have around 1 billion documents in 1-3 years.
Once more thing, how can I generate a unique ID for each line item? I know Mongo generates _id, but I mean a number 0, 1, 2, etc. which can be shown in invoices.
So, I'm not sure if MongoDB can handle it effectively.
Best,
Faheem
EDIT:
These are the reasons that I think this question is not a duplicate to the suggested duplicates.
Those questions are referring to ref vs embedding the document. I'm not.
The question is what will be the best way (in terms of performance and low latency) to store inline items, in a batch (array of object) or as a single document. I'm not concerned about references or anything here.

mongodb - fast pagination in a sharded cluster [duplicate]

This question already has answers here:
Implementing pagination in mongodb
(2 answers)
How does MongoDB sort records when no sort order is specified?
(2 answers)
Closed 5 years ago.
I'm running mongo 3.4 (w/ wiredtiger). Up to now I have been using the 'fast pagination' strategy specified in the following article (https://scalegrid.io/blog/fast-paging-with-mongodb), namely:
Retrieve the _id of the last document in the current page
Retrieve documents greater than this “_id” in the next page
//Page 1
db.users.find().limit(pageSize);
//Find the id of the last document in this page
last_id = ...
//Page 2
users = db.users.find({'_id'> last_id}). limit(10);
//Update the last id with the id of the last document in this page
last_id = ...
I am about to shard my collection in order to allow horizontal scaling. As part of enabling sharding, I am going to use a unique composite key (on fields "user_id" and "post_id") for a shard key. This will guarantee document uniquness across shards, and should allow for relatively good document distribution across shards.
But after I shard my collection, will I be able to use the above fast-pagination strategy? If not, is there a common solution?
Thanks

update() function disregards limit() in mongo [duplicate]

This question already has answers here:
How to limit number of updating documents in mongodb
(8 answers)
Closed 5 years ago.
Lets say I have a 10 documents of Item in the database.
Lets retrieve 3 documents of Item matching some condition using limit().
documents = Item.objects(somefield=somecondition).limit(3)
Now if I do
documents.update(), mongoengine updates all the documents in the database matched by the query not just the 3 documents I have limited my query to.
I also tried setting multi=False in the params, but then only one document gets updated.
Is there anyway to do update while querying itself instead of looping over the documents one by one?
As far as I know there is no available solution to your problem provided by MongoDB. However you could try something like this
documents.forEach(
function (e) {
e.field = 'value';
db.collection.save(e);
}
);

Mongo FIND Query to return Collections where Two Of Document's Fields Contain The Same Values (Were One is an Array) [duplicate]

This question already has answers here:
MongoDB Aggregation - match if value in array
(9 answers)
Closed 6 years ago.
Lets say I have a collection called Messages and a message can have multiple parents which can be found by checking if the message_id is in the parent_id array, where the parent_id is an array.
{
message_id : 22,
parent_id: [22,11],
}
How would I create a mongo query to find that?
Right now I have the following but it fails:
db.messages.find({this.message_id: { $in: this.parent_id}})
As you probably know mongo db does not support joins, I recommend you to do this at application level, using mongo shell you could run something like this.
//Get the message in which you are interested to get their parents
var child = db.messages.findOne({message_id: 22});
//Go with the array to search the parents.
db.messages.find({message_id : { $in : child.parent_id }});
I think you need to do in two steps, unless somebody knows a more elegant solution.