fetch data in meteor based on sub element - javascript

I have issue regarding publishing collection.
I have model of each document in collection.
{
"_id" : "8SwmMoMHTREDNDQTN",
"persons" : [
{
"userId" : "29pr753gmyDxQZzo6",
"lastReadTime":""
},
{
"userId" : "gWKbYKiQsFzdCTwkz",
"lastReadTime":""
}
]
}
Now i want to publish document which persons.userId have current user id
Here is my publish code
Meteor.publish("chatRooms", function(){
return userData.find({persons:{$elemMatch:{userId:this.userId}}});
});
But is it is nothing
i had also try below code
Meteor.publish("chatRooms", function(){
return userData.find({"persons.userId":this.userId});
});
But also getting result data 0
How can i publish this way?

Related

Sending the result of a mongoDB view to another collection ( INSERT....SELECT from SQL )

I could create a big query, and turn it into a view. Let's call it DBA_VIEW.
db.DBA_VIEW.find()
I'm using noSQLbooster to interact with mongodb and I'm trying to insert the result of this view into another collection.
I don't want to "right click > export" because I need to do this via noSQLbooster itself.
I've seen some querie sthat can do the trick, but, as a SQL SERVER dba, I think I can't get the logic behind, let's say, this one below:
db.full_set.find({date:"20120105"}).forEach(function(doc){
db.subset.insert(doc);
});
how can I use such approach to do my taks?
I just need the result of that view from a collection, to be inserted in another collection, then after this we are going to export the data into a .json file or even a .TXT.
You can design the query in the following way so that the output is directly inserted in a new collection('subset' in this example) without iterating through the result set.
db.full_set.aggregate([
{
$match:{
"date":"20120105"
}
},
{
$out:"subset"
}
])
If 'full_set' has the following documents:
{
"_id" : ObjectId("5d423675bd542251420b6b8e"),
"date" : "20130105",
"name" : "Mechanic1"
}
{
"_id" : ObjectId("5d423675bd542251420b6b8f"),
"date" : "20120105",
"name" : "Mechanic2"
}
{
"_id" : ObjectId("5d423675bd542251420b6b90"),
"date" : "20120105",
"name" : "Mechanic3"
}
With the above query, the 'subset' collection would be having the following documents:
{
"_id" : ObjectId("5d423675bd542251420b6b8f"),
"date" : "20120105",
"name" : "Mechanic2"
}
{
"_id" : ObjectId("5d423675bd542251420b6b90"),
"date" : "20120105",
"name" : "Mechanic3"
}

Meteor: Return only single object in nested array within collection

I'm attempting to filter returned data sets with Meteor's find().fetch() to contain just a single object, it doesn't appear very useful if I query for a single subdocument but instead I receive several, some not even containing any of the matched terms.
I have a simple mixed data collection that looks like this:
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e7"),
"name" : "Entertainment",
"items" : [
{
"_id" : ObjectId("57a38b5f2bd9ac8225caff06"),
"slug" : "this-is-a-long-slug",
"title" : "This is a title"
},
{
"_id" : ObjectId("57a38b835ac9e2efc0fa09c6"),
"slug" : "mc",
"title" : "Technology"
}
]
}
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e8"),
"name" : "Sitewide",
"items" : [
{
"_id" : ObjectId("57a38bc75ac9e2efc0fa09c9"),
"slug" : "example",
"name" : "Single Example"
}
]
}
I can easily query for a specific object in the nested items array with the MongoDB shell as this:
db.categories.find( { "items.slug": "mc" }, { "items.$": 1 } );
This returns good data, it contains just the single object I want to work with:
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e7"),
"items" : [
{
"_id" : ObjectId("57a38b985ac9e2efc0fa09c8")
"slug" : "mc",
"name" : "Single Example"
}
]
}
However, if a similar query within Meteor is directly attempted:
/* server/publications.js */
Meteor.publish('categories.all', function () {
return Categories.find({}, { sort: { position: 1 } });
});
/* imports/ui/page.js */
Template.page.onCreated(function () {
this.subscribe('categories.all');
});
Template.page.helpers({
items: function () {
var item = Categories.find(
{ "items.slug": "mc" },
{ "items.$": 1 } )
.fetch();
console.log('item: %o', item);
}
});
The outcome isn't ideal as it returns the entire matched block, as well as every object in the nested items array:
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e7"),
"name" : "Entertainment",
"boards" : [
{
"_id" : ObjectId("57a38b5f2bd9ac8225caff06")
"slug" : "this-is-a-long-slug",
"name" : "This is a title"
},
{
"_id" : ObjectId("57a38b835ac9e2efc0fa09c6")
"slug" : "mc",
"name" : "Technology"
}
]
}
I can then of course filter the returned cursor even further with a for loop to get just the needed object, but this seems unscalable and terribly inefficient while dealing with larger data sets.
I can't grasp why Meteor's find returns a completely different set of data than MongoDB's shell find, the only reasonable explanation is both function signatures are different.
Should I break up my nested collections into smaller collections and take a more relational database approach (i.e. store references to ObjectIDs) and query data from collection-to-collection, or is there a more powerful means available to efficiently filter large data sets into single objects that contain just the matched objects as demonstrated above?
The client side implementation of Mongo used by Meteor is called minimongo. It currently only implements a subset of available Mongo functionality. Minimongo does not currently support $ based projections. From the Field Specifiers section of the Meteor API:
Field operators such as $ and $elemMatch are not available on the client side yet.
This is one of the reasons why you're getting different results between the client and the Mongo shell. The closest you can get with your original query is the result you'll get by changing "items.$" to "items":
Categories.find(
{ "items.slug": "mc" },
{ "items": 1 }
).fetch();
This query still isn't quite right though. Minimongo expects your second find parameter to be one of the allowed option parameters outlined in the docs. To filter fields for example, you have to do something like:
Categories.find(
{ "items.slug": "mc" },
{
fields: {
"items": 1
}
}
).fetch();
On the client side (with Minimongo) you'll then need to filter the result further yourself.
There is another way of doing this though. If you run your Mongo query on the server, you won't be using Minimongo, which means projections are supported. As a quick example, try the following:
/server/main.js
const filteredCategories = Categories.find(
{ "items.slug": "mc" },
{
fields: {
"items.$": 1
}
}
).fetch();
console.log(filteredCategories);
The projection will work, and the logged results will match the results you see when using the Mongo console directly. Instead of running your Categories.find on the client side, you could instead create a Meteor Method that calls your Categories.find on the server, and returns the results back to the client.

AngularJS meteor mongo collection key value

I have this code
$scope.users = $meteor.collection( function() {
return AllClients.find({}, {name: 1, _id: 0});
});
I'm expecting to return a value like this
/* 1 */
{
"name" : "Samsung"
}
/* 2 */
{
"name" : "HP"
}
but it still returns a value like this
/* 1 */
{
"_id" : "SqFP23zTXo6MqDLxP",
"code" : "A100",
"name" : "Samsung",
"address" : "Korea"
}
/* 2 */
{
"_id" : "8QtNBoBGrvv5wWpuZ",
"code" : "B100",
"name" : "HP",
"address" : "USA"
}
Is this a bug? Or bad coding...
First off, if you don't want the other bits of information available on the client side, then you need to do the work on the server side. This is handled using publish methods.
Before all, remove autopublish:
> meteor remove autopublish
Then you can create publish method in your server folder:
Meteor.publish('clientNames', function() {
return AllCients.find({}, {fields: {name: 1} });
});
This publish method will find all of the clients and only allow the name field, keep in mind that you may still get the _id field, I believe it's always sent.
Then on your client side you'll need to subscribe to it:
$scope.$meteorSubscribe('clientNames').then(function() {
$scope.users = $scope.$meteorCollection(AllClients, false);
});
With meteor, when you access information form the client side, you only have access to what the server has allowed you to. In this case, you can request AllCients and not have all of the information because the server doesn't allow it.

Meteor Mongo not working but standalone MongoDB is

I have the following document
{
"_id" : ObjectId("5464e68481f8252e74f6e0ef"),
"message" : "Hello World!",
"timestamp" : ISODate("2014-11-13T17:12:36.547Z"),
"sender" : "D3EkLv8vatX3xfCGE",
"receiver" : [
{
"id" : "YzhZchz4AwCAd3q2R",
"seen" : false
},
{
"id" : "sm2W28EKYmHz29Aoj",
"seen" : false
}
],
"saved" : false
}
That's the basic structure. Now I want to get all the documents that have the current user's ID. The id can be in the sender field or one of the object in the receiver array. This works fine:
db.messages.find({ sender: "D3EkLv8vatX3xfCGE" })
But the following query doesn't work on the Meteor Mongo instance but does so in Mongo 2.6:
db.messages.find({ receiver: {id: "YzhZchz4AwCAd3q2R"} })
Is there any way I can get this to work. Also, the above query works on Minimongo but doesn't work on Meteor's bundled mongo(meteor mongo) which is currently at v2.4.9.
On mongo shell this query seems to work:
db.messages.find({ "receiver.id": "YzhZchz4AwCAd3q2R"} )
You can find all of the documents were a particular user is a receiver using receiver.id in your selector like so:
Messages.find({'receiver.id': 'YzhZchz4AwCAd3q2R'})
If you wanted to find all documents where a particular user was either a receiver or a sender you could do this:
var id = 'YzhZchz4AwCAd3q2R';
Messages.find({$or: [{'receiver.id': id}, {sender: id}]});

searching for nested elements in mongodb, using api nodejs

At this moment I have in Node.JS API written a function
Board.find({ users : req.user._id})
It will find all documents where is id of user inside of array users,
for example
So this function will find this document.
{
"_id" : ObjectId("5a7f4b46f489dd236410d88a"),
"name" : "first",
"users" : [
ObjectId("5a1db9e8db97d318ac70715d")
]
}
What If I will change array of users in document for array objects id
{
"_id" : ObjectId("5a7f4b46f489dd236410d77c"),
"name" : "second",
"users" : [
{ _id : ObjectId("5a1db9e8db97d318ac70715d") }
]
}
How to find now this document in this situation, using only req.user._id which is saved inside object of users[]?
we can find it somehow now or not??
You just need to change it as : Board.find({ 'users._id' : req.user._id})
You could also use:
db.collection_name.find( { "user": { "_id" : req.user._id } } )

Categories