In my MongoDatabase i have 5 elements.
First the _id then a follower with his idfollower and a following with his idfollowing.
now im trying to get only the IDFOLLOWING element
var userid = Relationships.findOne({follower: Meteor.user().username}).idfollowing;
this works!
Now i got 5 entries in the Database and the code only shows me 1 ID not 5
Give this a try:
// fetch an array of relationships where the current user is the follower
var selector = {follower: Meteor.user().username};
var relationships = Relationships.find(selector).fetch();
// create an array of ids? by extracting the idfollowing values from relationships
var userIds = _.pluck(relationships, 'idfollowing');
Here's the documentation for:
pluck
find
fetch
Related
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.
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/
I was checking the firebase docs looking for how to get the number of items saved in my DB according to a query and I looked at this piece of code that has (I think) exactly what I'm looking for, but dont know how to get the numbers of items displayed.
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.orderByChild("height").equalTo(0.6).on("child_added", function(snapshot) {
console.log(snapshot.key());
});
The console.log should display 2 items as there are 2 dinosaur with that height. So how can I get the number of items displayed to show it on the view?
I tried with console.log(snapshot.key().length); but it displays a wrong number
When you listen for the child_added event, the number of items in your callback will always be 1. This is great for when you (for example) want to add each item to a list. But it's not great when you want to count the number of items.
For operations that need to handle multiple items, it's easier to listen to the value event:
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.orderByChild("height").equalTo(0.6).on("value", function(snapshot) {
console.log(snapshot.numChildren());
})
This snippet also shows the other piece of the puzzle: DataSnapshot.numChildren().
You can try this:
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
var shortDinosaurs = ref.orderByChild("height").equalTo(0.6);
console.log(shortDinosaurs.length);
shortDinosaurs.on("child_added", function(snapshot) {
console.log(snapshot.key());
});
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}]
}
I'm trying to get all element of a parse Collection with Javascript.
collection.query.limit(1000);
doesn't work for me. query propiety doesn't exist in collection.
Thanks!
Per the docs here, it should be the other way around: https://parse.com/docs/js_guide#collections
var query = new Parse.Query("myClass");
query.limit(1000);
var collection = query.collection();