Mongoose update multiple documents - javascript

Actually i am using mongoDB, and i am able to update document with single ObjectID, so right now i want to perform update on multiple documents having different ObjectID, i did considerable search, came to know {multi:true} will help to update multiple documents.
Let's say i have
var uid = {'Userid': ObjectId(..)};
var id = {'Newid': Object(..)};
now i want to perform update on both of these different documents having different ObjectId' s total separatley, so far i tried this considering "xyz" and "abc" array's in Schema just for demo
var update_uid = {$push:{'xyz': some_id}};
var update_id = {$push:{'abc': some_other_id}};
Test.update(uid,id,update_uid,update_id,{multi:true},function(){..});
this is wrong i know, i mentioned it for the records just in case, Any help and suggestion would be much appreciated

If you have multiple ids that you want to match you can use the keywork $in which checks if there's any matches in the array. Now that you also have multiple properties that you want to check you can user the $or keyword, which will find a match if either of the objects in the array matches a document in the database.
var userIds = [{'Userid':ObjectId(..)}, {'Userid':ObjectId(..)}];
var newIds = [{'Newid': Object(..)}, {'Newid': Object(..)}];
var query = {
$or: [{$in: userIds}, {$in: newIds}]
}

Related

Firebase/Firestore start loop from certain document ID

I'm getting a collection from my Firestore database and adding the document values to an array in JS:
var data = myCollection();
var myArray = [];
data.forEach(function(data) {
var splitPath = data.name.split('/');
var documentId = splitPath[splitPath.length - 1];
var name = data.fields.name ? data.fields.name.stringValue : '';
var country = data.fields.vin ? data.fields.vin.stringValue : '';
myArray.push( [ documentId, name, country ] );
});
Suppose I know a document ID, is it possible to get the collection documents from that certain document ID?
I'm not sure if Firestore documents are ordered by date. I am trying to get the most recent documents from a certain document ID.
Suppose I know a document ID, is it possible to get the collection documents from that certain document ID?
When it comes to the Firebase console, all documents are sorted lexicographically by ID and this behavior can't be changed. When it comes to code, it's up to you to choose how to order the results.
I'm not sure if Firestore documents are ordered by date.
No, there is no time component inside the document IDs.
I am trying to get the most recent documents from a certain document ID.
In that case, the simplest solution would be to add a timestamp field in each document and order them according to that field.
However, Firebase Realtime Database pushed IDs do contain a time component. So if you want, you can add that data there. Both databases are working great together.
If you have multiple documents and you want to implement pagination, for example, you can use query cursors and use startAt() or starAfter() if you need that.
I don't know if this is exactly what you need but firebase docs has below example:Order and limit data
import { query, where, orderBy, limit } from "firebase/firestore";
const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
if you adjust where part to your id, then sort by date it should work.

Filtering with an array of objects Regex in mongodb and mongoose

I'm trying to create a dynamic filter that can be implemented using at least 3 letters. I've got a lot of fields which i will be filtering against.
As an example, If I am trying to find users by email, I want to be able to type "#gma" or at least "gma" and ideally, it should return an array of all users containing the specified filtering value. This should be the same when searching for properties like firstName and so on
My current solution only works if I provide a full value that matches what I already have in my database. e.g test#gmail.com for email or john for firstName. I want to be able to type jo for the latter.
const regexPattern = new RegExp(["^", filterUsersByValue, "$"].join(""), "i");
const filteredU = UserModel.find({ [filterUsersBy]: regexPattern})
If I've understood correctly, since you are using JS you can create the find object like this:
let findObj = {}
findObj[userKey] = {$regex:userFilter, $options:"i"}
const filteredU = UserModel.find(findObj)
This creates que object like this:
const userFilter = "gma"
const userKey = "email"
let findObj = {}
findObj[userKey] = {$regex:userFilter,$options:"i"}
console.log(findObj)
What is the same that in this query

MongoDB Bulk Insert Operation instead of For-Loop

Let's consider a social network to be built by NodeJS and MongoDB.
So, if a user creates a new post, it should be saved to his/her followers feed.
The straightforward implementation of this operation as follow (simplified):
var newPost="New Post";
//get list of followers of user 1
var listOfFollowers = followersCollection.find({u:"1"});
for(var i=0;i<listOfFollowers.length;i++){
var followerId = listOfFollowers[i]._id;
//insert new post of user 1 to every follower feed
feedsCollection.insertOne(
{ownerId:followerId,authorId:"1",content:newPost}
);
}
This, of course, has very bad performance in case of big numbers in followers count. How can do this with a single fast performing MongoDB query?
MongoDB provides bulk document insert functionality, check out this link - https://docs.mongodb.com/manual/reference/method/db.collection.initializeUnorderedBulkOp/
db.collection.initializeUnorderedBulkOp() It creates an unordered list of operations and mongodb executes this list in parallel, so it's fast and you don't have to take extra care of performance as mongo handles it.
For ordered insertions, you can use db.collection.initializeOrderedBulkOp().
e.g.
var newPost="New Post";
var bulk = db.followersCollection.initializeUnorderedBulkOp();
//get list of followers of user 1
var listOfFollowers = followersCollection.find({u:"1"});
for(var i=0;i<listOfFollowers.length;i++){
var followerId = listOfFollowers[i]._id;
//insert new post of user 1 to every follower feed
bulk.insert( {ownerId:followerId,authorId:"1",content:newPost});
}
bulk.execute();
If you are using Mongoose then checkout Mongoose docs for the same. In the above example, I have just trying to explain how you can do it using plain MongoDB.
Insert Many Read this document I think you will get the answer
Check this:
var newPost="New Post";
//Object Array
var collection = []
//get list of followers of user 1
var listOfFollowers = followersCollection.find({u:"1"});
for(var i=0;i<listOfFollowers.length;i++){
var followerId = listOfFollowers[i]._id;
collection.push({ownerId:followerId,authorId:"1",content:newPost})
}
feedsCollection.insert(collection); //Or use insertMany()
You can create your object array and insert it at once.
Check documentation :- https://docs.mongodb.com/manual/reference/method/db.collection.insert/#insert-multiple-documents
Even though this is a simple answer for your question, If the collection array has a large number of elements, there still might be performance issues. So the best way to handle this is using triggers. https://docs.mongodb.com/stitch/triggers/

MongoDB search string in array of objects

I have a structure in MongoDB that have different amounts of items in an array called "items". To make the search, I am using the following command, which first turns the contents into a string, as in this.items there is a different structure depending on the object:
db.getCollection('docs').find.('JSON.stringify(this.items[0].value).toLowerCase().indexOf("text")!=-1')
My problem is that as I do not know the amount of items that each document has, I would have to use a wildcard as this.items[*].value, but it does not work.
Does anyone know any solution, or have another idea for this?
You can use the $elemMatch (https://docs.mongodb.com/manual/reference/operator/projection/elemMatch/)
db.docs.find({items: {$elemMatch: {value: {$regex : "text"}}}});
So this query will find all documents with an item in the items array that contain the string "text" in the value property, after this operation you can count how much items the document has.
You can use dot notation of items.value to target the value field of all items elements, and a regular expression to perform the case-insensitive sub-string match:
db.getCollection('docs').find({ 'items.value': /text/i })
You can iterate each document and apply the indexOf, something like this..
var cursor = db.getCollection('docs').find({}); // get all docs
var newOut = []; // new array of items if match with some condition
while ( cursor.hasNext() ){ // iterate all docs
var doc = cursor.next(); // get the document in focus
doc.items.forEach(function(item){ // iterate the items of doc.items
if ( item.toLowerCase().indexOf("text") !== -1 ) // check if text exists in array
newOut.push(item); // add to new array
});
};
printjson(newOut);

MongoDB count and results

I would really appreciate if someone could help me with something: I need to make a normal query to the database but, as my collection is very large (10000 documents) I need to do the query and use $limit and $skip. That part I solved but now I want to have a count to all the documents, even if the returned ones are less. The output should be something like this:
{
count: 1150,
data: [/*length of 50*/]
}
Could anyone help please? Thank you very much.
Since you mentioned you are making a normal query, its not wise to go for aggregation. find() will be a much better option here. Instead you can use the find query itself. The commands to do this in mongoDB console is shown below:
> var docs = db.collection.find().skip(10).limit(50)
> docs.count()
189000
> docs.length()
50
You can do this using one query itself. In Node.js and Express.js, you will have to use it like this to be able to use the "count" function along with the toArray's "result".
var curFind = db.collection('tasks').find({query});
Then you can run two functions after it like this (one nested in the other)
curFind.count(function (e, count) {
// Use count here
curFind.skip(0).limit(10).toArray(function(err, result) {
// Use result here and count here
});
});
I don't think it is possible in a single query to get the total count of the result along with the paginated data without using aggregation.
You can probably achieve this via aggregation but since you mentioned, your collection is very large, you should avoid it and break the query into two parts. I'm providing you an example of considering user collection having a rating field with more than 10,000 records:
var finalResult = {};
var query = {
rating: {$gt: 2}
};
// Get first 50 records of users having rating greater than 2
var users = db.user.find(query).limit(50).skip(0).toArray();
// Get total count of users having rating greater than 2
var totalUsers = db.user.cound(query);
finalResult.count = totalUsers;
finalResult.data = users;
And your final output can be like:
finalResult == {
count: 1150,
data: [/*length of 50 users*/]
}
Hope, this make sense to you. Some of the famous technologies like Grails internally do that to achieve pagination.
Another cleaner approach could be:
var finalResult = {};
var query = {
rating: {$gt: 2}
};
var cursor = db.user.find(query).limit(50).skip(0);
// Get total count of users having rating greater than 2
// By default, the count() method ignores the effects of the cursor.skip() and cursor.limit()
finalResult.count = cursor.count();;
finalResult.data = cursor.toArray();
As mentioned by Sarath Nair you can use count, as it ignores skip and limit. Check: MongoDB Count
By the way, this is duplication from another StackOverflow question: Limiting results in MongoDB but still getting the full count?

Categories