我正在尝试find通过collectionMongoose 的属性执行本机MongoDB 查询Model.我没有提供回调,所以我希望find返回一个Cursor对象,但它会返回undefined.根据该猫鼬文档,正在使用的驱动程序是通过访问YourModel.collection,如果我切换到纯粹使用本机驱动程序代码find并返回Cursor,所以我想不通这是怎么回事.
这是一个重现问题的代码片段:
var db = mongoose.connect('localhost', 'test');
var userSchema = new Schema({
username: String,
emailAddress: String
});
var User = mongoose.model('user', userSchema);
var cursor = User.collection.find({});
// cursor will be set to undefined
Run Code Online (Sandbox Code Playgroud)
我试图通过node-inspector进入代码,但它并没有让我这么做.知道我做错了什么吗?
Documents.update(
{_id: Session.get("current_document_id")},
{$push: {schema: {type: "text", size: size, name: name, label: label}}}
);
Run Code Online (Sandbox Code Playgroud)
上面的查询是Meteor集合,'Documents.update'映射到MongoDB文档中的'db.documents.update'(http://docs.mongodb.org/manual/applications/update/).通过该查询,我可以在主文档中添加模式文档.子文档存储在一个数组中:
Document:
schema:
array:
{type: "text", size: 6, name: "first_name", label: "First name"},
{type: "text", size: 6, name: "last_name", label: "Last name"}
Run Code Online (Sandbox Code Playgroud)
我想使用此查询修改子文档的名称和大小属性:
Documents.update(
{_id: Session.get("current_document_id"), 'schema' : "first_name"},
{$push: {schema: {type: "text", size: 7, name: name, label: "First Name2"}}}
);
Run Code Online (Sandbox Code Playgroud)
但是该操作直接在模式下附加一个新对象并删除该数组:
Document:
schema:
{type: "text", size: 7, name: "first_name", label: "First Name2"}
Run Code Online (Sandbox Code Playgroud)
如何修改查询以更改属性以避免此问题?查询后我想有这个文件:
Document:
schema:
array:
{type: "text", size: 7, name: "first_name", label: "First name2"}, …Run Code Online (Sandbox Code Playgroud) Team.find({
'_id': { $in: [
teamIds
] }
}, function(err, teamData) {
console.log("teams name " + teamData);
});
Run Code Online (Sandbox Code Playgroud)
这段代码给了我们未定义的返回..但在var teamIds是这样的:
545646d5f5c1cce828982eb7,
545646d5f5c1cce828982eb8,
54564af5c9ddf61e2b56ad1e,
54564c1f1de201782bcdb623,
54564d2fc660a7e12be6c7a2,
54564df985495f142c638f9f,
54564eadb511f1792c9be138,
54564ec40cf6708a2cd01c81,
54564ee495f4aea22cf23728
Run Code Online (Sandbox Code Playgroud)
有人看到错误吗?
所以根据MongoDB文档,
如果文档字段包含单词blueberry,则对术语blue的搜索将与文档不匹配
这对我的用例很有用,这就是我想要发生的事情.但是,给定以下数据库条目:
> db.test.drop()
> db.test.insert({ "t" : "Men's Fashion" })
> db.test.insert({ "t" : "Women's Fashion" })
> db.test.ensureIndex({ "t" : "text" })
Run Code Online (Sandbox Code Playgroud)
搜索Men's会返回预期结果:
> db.test.find({ "$text" : { "$search" : "\"Men's\"" } }, { "_id" : 0 })
{ "t" : "Men's Fashion" }
Run Code Online (Sandbox Code Playgroud)
然而,搜索整个短语Men's Fashion,意外地也回归女性时尚:
> db.test.find({ "$text" : { "$search" : "\"Men's Fashion\"" } }, { "_id" : 0 })
{ "t" : "Women's Fashion" }
{ "t" : "Men's Fashion" }
Run Code Online (Sandbox Code Playgroud)
我也尝试过"\"Men's\"\"Fashion\""同样的结果.是否有一个解决方法/技巧来使完整的短语只返回整个单词匹配? …
我有一个包含以下格式的条目的集合:
{
"_id" : ObjectId("5538e75c3cea103b25ff94a3"),
"userID" : "USER001",
"userName" : "manish",
"collegeIDs" : [
"COL_HARY",
"COL_MARY",
"COL_JOHNS",
"COL_CAS",
"COL_JAMES",
"COL_MARY",
"COL_MARY",
"COL_JOHNS"
]
}
Run Code Online (Sandbox Code Playgroud)
我需要找出那些重复的大学ID.所以结果应该给出"COL_MARY","COL_JOHNS",如果可能的话,给出重复计数.请给mongo查询找到它.
我有以下代码,我是mongodb的新手,我需要帮助找到集合中的特定元素.
using MongoDB.Bson;
using MongoDB.Driver;
namespace mongo_console {
public class User {
public ObjectId Id { get; set; }
public string name { get; set; }
public string pwd { get; set; }
}
class Program {
static void Main(string[] args)
{
MongoClient client = new MongoClient();
MongoServer server = client.GetServer();
MongoDatabase db = server.GetDatabase("Users");
MongoCollection<User> collection = db.GetCollection<User>("users");
User user = new User
{
Id = ObjectId.GenerateNewId(),
name = "admin",
pwd = "admin"
};
User user2 = new User …Run Code Online (Sandbox Code Playgroud) 我在页面上有一个简单的angular-kendo ComboBox,没有最初选择的值.它应该placeholder在那种情况下显示文本,而是显示它? undefined:undefined ?
HTML
<select kendo-combo-box ng-model="Project" k-options='projectOptions'></select>
Run Code Online (Sandbox Code Playgroud)
JS
app.controller('MyCtrl', function($scope) {
$scope.projectData = [
{name: 'Bob', value: 1},
{name: 'Tom', value: 2}
];
$scope.projectOptions = {
placeholder: "'Select...'",
dataTextField: 'name',
dataValueField: 'value',
dataSource: {
data: $scope.projectData
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是一个显示问题的plunker.谁能发现原因?
这曾经在较旧版本的angular-kendo中工作,但它在当前版本中不起作用.
我的猫鼬模式如下:
var DSchema = new mongoose.Schema({
original_y: {type: Number},,
new_y: {type: Number},,
date: {type: Date},
dummy: [dummyEmbeddedDocuments]
}, toObject: { virtuals: true }, toJSON: { virtuals: true}
});
DSchema.virtual('dateformatted').get(function () {
return moment(this.date).format('YYYY-MM-DD HH:mm:ss');
});
module.exports = mongoose.model('D', DSchema);
Run Code Online (Sandbox Code Playgroud)
我的架构中的文档如下:
{
id:1,
original_y: 200,
new_y: 140,
date: 2015-05-03 00:00:00.000-18:30,
dummy: [
{id:1, storage:2, cost: 10},
{id:2, storage:0, cost: 20},
{id:3, storage:5, cost: 30},
]
}
Run Code Online (Sandbox Code Playgroud)
我的查询:
Item.aggregate([
{
"$match": {
"dummy.storage": {"$gt": 0}
}
},
{
"$unwind": "$dummy"
},
{ …Run Code Online (Sandbox Code Playgroud) 基本上我正在尝试根据其唯一的objectID(_id)更新Mongo数据库上的多个对象.我尝试了以下但他们没有工作:
var new_arr = [];
for (var i = 0; i < req.body.length; i++) {
// req.body is an array received from the POST request.
new_arr.push({"_id": new mongodb.ObjectID(req.body[i])})
}
console.log(new_arr);
// new_arr is not accepted since an object is expected.
job_applications.updateMany(
new_arr
,
{
$set: {"application_status": "rejected"}
}, function (err, results) {
console.log(results);
console.log(err);
if (!err) {
console.log('success with reject');
res.sendStatus(200);
}
}
);
Run Code Online (Sandbox Code Playgroud)
我也试过以下没有运气.
var job_applications = req.app.locals.job_applications;
var new_arr = [];
for (var i = 0; i < …Run Code Online (Sandbox Code Playgroud) 先前说过我已将文档插入到mongo集合中.
MongoClient.connect(url, function(err,db){
if(err) {throw err;}
else {
document = {action: "alert",
protocol: "udp",
port: "80",
_id: "12" }
var collection = db.collection("connections");
collection.insertOne(document, function(err,result){
if (err) {throw err;}
else {
console.log("Successful")
db.close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想更新协议字段.到目前为止我没有运气的是
MongoClient.connect(url, function(err,db){
if (err) { throw err; }
else {
var collection = db.collection("connections");
collection.findOneAndUpdate({_id: "12"}, {$set: {protocol: "http"}}, {new: true}, function(err,doc) {
if (err) { throw err; }
else { console.log("Updated"); }
});
}
});
Run Code Online (Sandbox Code Playgroud)
我是否将错误的参数传递给findOneAndUpdate方法?我正确连接到数据库.