I am using MeteorJS as my framework.
In my imports/api/collections/categories.js file, I have:
export const Photos = new Mongo.Collection('photos');
On the server side, I have:
Photos = new Mongo.Collection('photos');
How can I insert.before so all my documents have an extra field when they get added to the database?
Before I was exporting for React, I had:
Photos = new Mongo.Collection('photos');
Photos.before.insert(function(userId,doc){
doc.createdAt=Date.now();doc.status=0;
});
But that doesn't work with:
export const Photos...
Ideas how I achieve the insert before?
Okay, as it turns out, I can do what I did before:
Photos.before.insert(function(userId,doc){doc.status=0;});
I just had to bring in this package into Meteor:
matb33:collection-hooks
Related
I want to migrate only one table to drop whenever sails lift I wanted to delete that table. How it is possible to do? Or is there any way to delete table after sails lift?
I already tried to add migrate : 'drop' in that particular model but it won't work.
I am currently using sails 1.0 version
I found a way.
Sails Bootstrap
This runs before sails lifted
I drop the table here.
config/bootstrap.js
module.exports.bootstrap = async function() {
await Users.destroy({});
};
Manually Delete
if you want to delete collection manually, you can use this command in shell.
db.collection.drop()
check this link for more detail click here
Delete via Sails.js
to use low-level native code for mongodb
// Get access to the native MongoDB client via the default Sails datastore.
var db = sails.getDatastore().manager;
db.collection('users').drop()
find link here link
In sails.js structure there is one file called bootstrap which located in config folder.
bootstrap file execute before sails server is lift.
so whatever you wan't to do before server lifting you can write code in this bootstrap file.
module.exports.bootstrap = function(callback) {
Users.destroy({},(error,response)=>
{
if(error) { console.log("error while deleting table"); }
else { callback(); }
});
};
I am connecting to some remote collections via the following pattern:
let remoteDB = new MongoInternals.RemoteCollectionDriver("mongodb://localhost:7071/meteor");
export const RemoteCollection = new Mongo.Collection("remoteCollection", {_driver:remoteDB});
Which is working fine for normal collections.
However, there is a FilesCollection (via ostrio:files) that also exists in this remote DB.
The FileCollection constructor does not let me pass the _driver options, so I am asking if someone has managed to load the remote files via gridfs, before opening a ticket on the project.
Looking at the source of Meteor-Files, the Mongo.Collection is added in lines 126-130 of server.js and lines 73-77 of client.js without any options being passed in.
What you can do is pass your own RemoteCollection into the FilesCollection constructor and the collection will use your collection and it's remote.
let remoteDB = new MongoInternals.RemoteCollectionDriver("mongodb://localhost:7071/meteor");
export const RemoteCollection = new Mongo.Collection("remoteCollection", {_driver:remoteDB});
export const RemoteFilesCollection = new FilesCollection({
collectionName: "remoteCollection",
collection: RemoteCollection
});
You'll also need to add all the extra code for GridFS integration to Meteor-Files: https://github.com/VeliovGroup/Meteor-Files/wiki/GridFS-Integration
WebStorm gives me a warning of "Unrecognized function or method" on functions like:
Schema.find() [find() not recognized]
Schema.aggregate() [aggregate() not recognized]
Schema.findOneAndUpdate() [findOneAndUpdate() not recognized]
I've tried to Enabling the NodeJs core libraries and to install
mongodb-DefinitelyType
mongoose-DefinitelyType
mongoose-auto-increment-DefinitelyType
mongoose-deep-populate-DefinitelyType
mongoose-DefinitelyType
mongoose-mock-DefinitelyType
under Preferences > JavaScript > Libraries
But this has not solved my problem. Does someone knows a solution?
This issue is tracked as https://youtrack.jetbrains.com/issue/WEB-17099; please see https://youtrack.jetbrains.com/issue/WEB-17099#comment=27-1441265 for possible workaround
This is possible solution working for me without any issues.
Moving the relative path out of the require() statement as follows.
const PATH = '../models/';
const User = require(PATH + 'user');
Alternatively
Do not import Schema separately.
Just import mongoose like this
const mongoose = require('mongoose');
and use mongoose.Schema to access Schema
I don't know the reason but somehow this works:
export
module.exports.User = User; // your model
import
const User = require("../dbSchema/user.js").User;
Note that the schema is inserted correctly. Don't forget to check your Schema.
Sample
module.exports = mongoose.model('SampleCollection', SampleSchema);
I'm quite new in javascript programing, so I really appreciate any help.
I'm using:
Meteor (official Windows port) latest version 1.1.0.2 with Iron Router and Autopublish package
What I'm trying to do, shouldn't be hard to do, but something is missing to me.
I just want to load data from Mongo DB collection
Movies = new Mongo.Collection('movies');
into my template in /client folder
<template name="movie_template">
<p>Dynamic page test with movieID {{id}}</p>
<h1>{{name}}</h1>
<p>{{year}}</p>
</template>
my router.js file based in /lib folder in root of my Meteor project
Router.route('/movies/:movieId', function() {
this.render('movie_template', {
waitOn: function() {
return Meteor.subscribe('moviesDetails', this.params.movieId);
},
data: function() {
return Movies.findOne({id: this.params.movieId});
}
});
});
publications.js in /server folder
Meteor.publish('movieDetails', function(movieID) {
check(movieID, Number);
return Movies.find({id: movieID});
});
Only one thing what I get is paragraph text without ID. What I'm doing wrong?
Side question:
Do I have to use publish() function while I'm using Autopublish package?
In that case just use Movies.find() instead subscribe()?
NOTE: this is my Movie object field keys.
{_id, id, name, year}
#below9k is correct and you should be using _id rather than id.
To answer your side question, with the autopublish package it is not necessary to do either Meteor.publish and Meteor.subscribe
I am using mongodb to store data and i wrote a simple js script using mongoskin to query and retrieve data from a collection and it works fine...
var db = require('mongoskin').db('winter.ceit.uq.edu.au/openbeacon');
var time = 0;
var tagid = 1101;
db.collection('set1').find({tag : {'$elemMatch': {id: tagid,name :"reader07"}}},function(err, result) {
if (err) throw err;
result.each(function(err, band) {
console.log(band.tag);
time += band.time;
});
});
However i need a way to integrate this functionality into a webpage...so say i press a button on the page, the js script runs and the queried data is displayed on the webpage. When i try using this javascript in a HTML file, it erros saying "module not found" since im referencing the index.js for mongoskin and mongodb as the source in my html file.....
Please lemme know what are the ways (preferably the simplest ways) to do this.
thank you.
Try looking for example applications
here is one
https://github.com/christkv/tic-tac-toe-steps