How can I get sliced documents - javascript

I have many documents like this:
How can I get, say, 11 documents without everything expect content.headline, content.description, content.tags and id fields via _id.
Simply put I have array of _id and I want to get news fields via this array.
Array example:
[
6002baa15e0b210f9ccd3060,
6002baa25e0b210f9ccd3062,
6002ba9e5e0b210f9ccd305c,
6002baa05e0b210f9ccd305f,
6002ba9f5e0b210f9ccd305d,
6002ba9e5e0b210f9ccd305b,
6002baa15e0b210f9ccd3061,
6002ba9c5e0b210f9ccd3059,
6002ba8d5e0b210f9ccd3058,
6002ba9d5e0b210f9ccd305a,
6002baa05e0b210f9ccd305e
]

You use the same dot notation as it is object.
You do normal find with project and limit.
db.coll.find({},
{
"content.headline":1,
"another_field":1
}
).limit(11)

Related

How do I store an array in a PSQL, where it is passed as a parameter $1 to the db query

I am passing a one-dimensional array of three strings to the function, it looks like this going in:
[ '#masprofundo', '#massensual', '#eclectic' ]
The data column is declared thus:
tags TEXT []
This is my function:
const query = `INSERT INTO posts (posted_at, message, link, tags)
VALUES (TO_TIMESTAMP($1, 'DD/MM/YYYY HH24:MI'), $2, $3, ARRAY [$4])
RETURNING tags;`;
const params = [timestamp, message, link, tags];
Now, postgres believes I want to insert an array containing one item, which is a string of all the values in my tags array. It looks like this:
{ tags: [ '{"#masprofundo","#massensual","#eclectic"}' ] }
What I want to know is, how do I prevent this behaviour, where postGres adds an unnecessary extra layer of quotation marks and parentheses? For further clarification; this is what the row looks like in my terminal.
{"{\"#masprofundo\",\"#massensual\",\"#eclectic\"}"}
I have looked at the docs, and tried a dozen variations on ARRAY[$4]. From what I can see, the docs do not elaborate on inserting arrays as variables. Is there some destructuring that needs to happen? The arrays I pass will be of varying size, and sometimes empty.
Any help is much appreciated.

How can I manually access a third-dimension javascript Array

I've been trying to access a third level node in an array using the indexes in it, but I can't access it, I tried a lot of ways that I found here on SO but I don't want to iterate through it, I want to get it manually.
var data = [
{code:1,
label:'John Doe',
tasks:[{
code:1,
label: 'AnyProject',
starts:'2016/1/25',
ends:'2016/2/25'}]
}];
What I want to do (theoretically):
data[0].tasks.code
data[0].tasks[0].code
tasks is an Array so you need to access it like an array.
data[0].tasks[0].code
Inside data array you have tasks and inside task array you have property code.
[] is an array you can use index to look inside.
{} is an object you can access using .

extracting a list of 2 property values

I have an object array:
user :[
{
name: String,
username: String
}
]
I want to view every change either to name or username.
I found underscore _.pluck only does the trick for one property (_.pluck(user, 'name')
Is there another way to have the list of both values?
With pluck you can only use one property, it simply isn't made to retrieve multiple. The method you would want to use is map, as suggested in this relevant question + answer: How to pluck multiple attributes from a Backbone collection?
Assuming you want the following output [['nameA','usernameA'],['nameB','usernameB'],...]], you could use map in the following manner:
var myResult = users.map(function(user) {
return [user.name, user.username];
});
NOTE: I changed the variable user to users to make more sense with your data.

make mongoDB replace single value array with string

I'm very new to mongoDB and am trying to insert an object to the database. (Wow, much more fun than mySQL...). I'm using strongloop's loopback framework and its mongoDB connector.
The object is a xml2js parsed xml message I receive, after parsing and inserting to mongo it looks like this:
{
"_id": ObjectID("55c61ee9391da88435c5753f"),
"offerChange": [
{
"foo": [
"bar"
],
"baz": [
"foo"
]
}
]
}
as you can see, all the key's values are arrays, though all of them contain only one value. Obviously I can convert them to strings before the insert or during xml parsing, but that would require looping over all the object's keys or more work for the worker in the first place, which I'd like to avoid. The real object is much bigger than above shown.
Is there a way to tell mongoDB to automatically convert arrays that have only one value to a string before or after the document gets created?
You need not loop through and convert from array to string in application/database. xml2js provide a neat way to return string if there is only one item and array if there are more items.
explicitArray (default: true): Always put child nodes in an array if
true; otherwise an array is created only if there is more than one.
Initiate your parser as follows
parseString(xml, {explicitArray: false}, function (err, result) {
});

cross-referencing two firebase arrays to obtain data

I was wondering if there's an easy way to pull out a value from one array based on its corresponding name.
Here, I've obtained the user email from the "coupons" collection. Now I would like to search through the "users" collection, find similar email, and output its corresponding "Name" ("Wes Haque Enterprises") to a $scope variable.
I already have references to both collections and $scope objects which have those references stored.
I just wanted to know if there's an easy way to traverse through the $scope.users object looking for the string "wes#wes.com" and then extracting "Wes Haque Enterprises" from it. Thanks.
Assuming that you don't really want to iterate (traverse?) over a bunch of arrays but instead query for the data you need...
You can query the users node for the data you need. In MacOS:
FQuery *allUsers = [usersRef queryOrderedByChild:#"emailAddress"];
FQuery *thisUser = [allUsers queryEqualToValue:#"wes#wes.com"];
[thisUser observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
for ( FDataSnapshot *child in snapshot.children) {
NSLog(#"%#", child);
}
}]
The result of the query will contain "Wes Haque Enterprises"
Or
ref.orderByChild("emailAddress").equalTo("wes#wes.com").on("child_added", function(snapshot) {
console.log(snapshot.key());
});

Categories

Resources